You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

590 lines
24 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
  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. */
  11. #include "call/bitrate_allocator.h"
  12. #include <algorithm>
  13. #include <cmath>
  14. #include <memory>
  15. #include <utility>
  16. #include "absl/algorithm/container.h"
  17. #include "api/units/data_rate.h"
  18. #include "api/units/time_delta.h"
  19. #include "rtc_base/checks.h"
  20. #include "rtc_base/logging.h"
  21. #include "rtc_base/numerics/safe_minmax.h"
  22. #include "system_wrappers/include/clock.h"
  23. #include "system_wrappers/include/field_trial.h"
  24. #include "system_wrappers/include/metrics.h"
  25. namespace webrtc {
  26. namespace {
  27. using bitrate_allocator_impl::AllocatableTrack;
  28. // Allow packets to be transmitted in up to 2 times max video bitrate if the
  29. // bandwidth estimate allows it.
  30. const uint8_t kTransmissionMaxBitrateMultiplier = 2;
  31. const int kDefaultBitrateBps = 300000;
  32. // Require a bitrate increase of max(10%, 20kbps) to resume paused streams.
  33. const double kToggleFactor = 0.1;
  34. const uint32_t kMinToggleBitrateBps = 20000;
  35. const int64_t kBweLogIntervalMs = 5000;
  36. double MediaRatio(uint32_t allocated_bitrate, uint32_t protection_bitrate) {
  37. RTC_DCHECK_GT(allocated_bitrate, 0);
  38. if (protection_bitrate == 0)
  39. return 1.0;
  40. uint32_t media_bitrate = allocated_bitrate - protection_bitrate;
  41. return media_bitrate / static_cast<double>(allocated_bitrate);
  42. }
  43. bool EnoughBitrateForAllObservers(
  44. const std::vector<AllocatableTrack>& allocatable_tracks,
  45. uint32_t bitrate,
  46. uint32_t sum_min_bitrates) {
  47. if (bitrate < sum_min_bitrates)
  48. return false;
  49. uint32_t extra_bitrate_per_observer =
  50. (bitrate - sum_min_bitrates) /
  51. static_cast<uint32_t>(allocatable_tracks.size());
  52. for (const auto& observer_config : allocatable_tracks) {
  53. if (observer_config.config.min_bitrate_bps + extra_bitrate_per_observer <
  54. observer_config.MinBitrateWithHysteresis()) {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. // Splits |bitrate| evenly to observers already in |allocation|.
  61. // |include_zero_allocations| decides if zero allocations should be part of
  62. // the distribution or not. The allowed max bitrate is |max_multiplier| x
  63. // observer max bitrate.
  64. void DistributeBitrateEvenly(
  65. const std::vector<AllocatableTrack>& allocatable_tracks,
  66. uint32_t bitrate,
  67. bool include_zero_allocations,
  68. int max_multiplier,
  69. std::map<BitrateAllocatorObserver*, int>* allocation) {
  70. RTC_DCHECK_EQ(allocation->size(), allocatable_tracks.size());
  71. std::multimap<uint32_t, const AllocatableTrack*> list_max_bitrates;
  72. for (const auto& observer_config : allocatable_tracks) {
  73. if (include_zero_allocations ||
  74. allocation->at(observer_config.observer) != 0) {
  75. list_max_bitrates.insert(
  76. {observer_config.config.max_bitrate_bps, &observer_config});
  77. }
  78. }
  79. auto it = list_max_bitrates.begin();
  80. while (it != list_max_bitrates.end()) {
  81. RTC_DCHECK_GT(bitrate, 0);
  82. uint32_t extra_allocation =
  83. bitrate / static_cast<uint32_t>(list_max_bitrates.size());
  84. uint32_t total_allocation =
  85. extra_allocation + allocation->at(it->second->observer);
  86. bitrate -= extra_allocation;
  87. if (total_allocation > max_multiplier * it->first) {
  88. // There is more than we can fit for this observer, carry over to the
  89. // remaining observers.
  90. bitrate += total_allocation - max_multiplier * it->first;
  91. total_allocation = max_multiplier * it->first;
  92. }
  93. // Finally, update the allocation for this observer.
  94. allocation->at(it->second->observer) = total_allocation;
  95. it = list_max_bitrates.erase(it);
  96. }
  97. }
  98. // From the available |bitrate|, each observer will be allocated a
  99. // proportional amount based upon its bitrate priority. If that amount is
  100. // more than the observer's capacity, it will be allocated its capacity, and
  101. // the excess bitrate is still allocated proportionally to other observers.
  102. // Allocating the proportional amount means an observer with twice the
  103. // bitrate_priority of another will be allocated twice the bitrate.
  104. void DistributeBitrateRelatively(
  105. const std::vector<AllocatableTrack>& allocatable_tracks,
  106. uint32_t remaining_bitrate,
  107. const std::map<BitrateAllocatorObserver*, int>& observers_capacities,
  108. std::map<BitrateAllocatorObserver*, int>* allocation) {
  109. RTC_DCHECK_EQ(allocation->size(), allocatable_tracks.size());
  110. RTC_DCHECK_EQ(observers_capacities.size(), allocatable_tracks.size());
  111. struct PriorityRateObserverConfig {
  112. BitrateAllocatorObserver* allocation_key;
  113. // The amount of bitrate bps that can be allocated to this observer.
  114. int capacity_bps;
  115. double bitrate_priority;
  116. };
  117. double bitrate_priority_sum = 0;
  118. std::vector<PriorityRateObserverConfig> priority_rate_observers;
  119. for (const auto& observer_config : allocatable_tracks) {
  120. priority_rate_observers.push_back(PriorityRateObserverConfig{
  121. observer_config.observer,
  122. observers_capacities.at(observer_config.observer),
  123. observer_config.config.bitrate_priority});
  124. bitrate_priority_sum += observer_config.config.bitrate_priority;
  125. }
  126. // Iterate in the order observers can be allocated their full capacity.
  127. // We want to sort by which observers will be allocated their full capacity
  128. // first. By dividing each observer's capacity by its bitrate priority we
  129. // are "normalizing" the capacity of an observer by the rate it will be
  130. // filled. This is because the amount allocated is based upon bitrate
  131. // priority. We allocate twice as much bitrate to an observer with twice the
  132. // bitrate priority of another.
  133. absl::c_sort(priority_rate_observers, [](const auto& a, const auto& b) {
  134. return a.capacity_bps / a.bitrate_priority <
  135. b.capacity_bps / b.bitrate_priority;
  136. });
  137. size_t i;
  138. for (i = 0; i < priority_rate_observers.size(); ++i) {
  139. const auto& priority_rate_observer = priority_rate_observers[i];
  140. // We allocate the full capacity to an observer only if its relative
  141. // portion from the remaining bitrate is sufficient to allocate its full
  142. // capacity. This means we aren't greedily allocating the full capacity, but
  143. // that it is only done when there is also enough bitrate to allocate the
  144. // proportional amounts to all other observers.
  145. double observer_share =
  146. priority_rate_observer.bitrate_priority / bitrate_priority_sum;
  147. double allocation_bps = observer_share * remaining_bitrate;
  148. bool enough_bitrate = allocation_bps >= priority_rate_observer.capacity_bps;
  149. if (!enough_bitrate)
  150. break;
  151. allocation->at(priority_rate_observer.allocation_key) +=
  152. priority_rate_observer.capacity_bps;
  153. remaining_bitrate -= priority_rate_observer.capacity_bps;
  154. bitrate_priority_sum -= priority_rate_observer.bitrate_priority;
  155. }
  156. // From the remaining bitrate, allocate the proportional amounts to the
  157. // observers that aren't allocated their max capacity.
  158. for (; i < priority_rate_observers.size(); ++i) {
  159. const auto& priority_rate_observer = priority_rate_observers[i];
  160. double fraction_allocated =
  161. priority_rate_observer.bitrate_priority / bitrate_priority_sum;
  162. allocation->at(priority_rate_observer.allocation_key) +=
  163. fraction_allocated * remaining_bitrate;
  164. }
  165. }
  166. // Allocates bitrate to observers when there isn't enough to allocate the
  167. // minimum to all observers.
  168. std::map<BitrateAllocatorObserver*, int> LowRateAllocation(
  169. const std::vector<AllocatableTrack>& allocatable_tracks,
  170. uint32_t bitrate) {
  171. std::map<BitrateAllocatorObserver*, int> allocation;
  172. // Start by allocating bitrate to observers enforcing a min bitrate, hence
  173. // remaining_bitrate might turn negative.
  174. int64_t remaining_bitrate = bitrate;
  175. for (const auto& observer_config : allocatable_tracks) {
  176. int32_t allocated_bitrate = 0;
  177. if (observer_config.config.enforce_min_bitrate)
  178. allocated_bitrate = observer_config.config.min_bitrate_bps;
  179. allocation[observer_config.observer] = allocated_bitrate;
  180. remaining_bitrate -= allocated_bitrate;
  181. }
  182. // Allocate bitrate to all previously active streams.
  183. if (remaining_bitrate > 0) {
  184. for (const auto& observer_config : allocatable_tracks) {
  185. if (observer_config.config.enforce_min_bitrate ||
  186. observer_config.LastAllocatedBitrate() == 0)
  187. continue;
  188. uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
  189. if (remaining_bitrate >= required_bitrate) {
  190. allocation[observer_config.observer] = required_bitrate;
  191. remaining_bitrate -= required_bitrate;
  192. }
  193. }
  194. }
  195. // Allocate bitrate to previously paused streams.
  196. if (remaining_bitrate > 0) {
  197. for (const auto& observer_config : allocatable_tracks) {
  198. if (observer_config.LastAllocatedBitrate() != 0)
  199. continue;
  200. // Add a hysteresis to avoid toggling.
  201. uint32_t required_bitrate = observer_config.MinBitrateWithHysteresis();
  202. if (remaining_bitrate >= required_bitrate) {
  203. allocation[observer_config.observer] = required_bitrate;
  204. remaining_bitrate -= required_bitrate;
  205. }
  206. }
  207. }
  208. // Split a possible remainder evenly on all streams with an allocation.
  209. if (remaining_bitrate > 0)
  210. DistributeBitrateEvenly(allocatable_tracks, remaining_bitrate, false, 1,
  211. &allocation);
  212. RTC_DCHECK_EQ(allocation.size(), allocatable_tracks.size());
  213. return allocation;
  214. }
  215. // Allocates bitrate to all observers when the available bandwidth is enough
  216. // to allocate the minimum to all observers but not enough to allocate the
  217. // max bitrate of each observer.
  218. // Allocates the bitrate based on the bitrate priority of each observer. This
  219. // bitrate priority defines the priority for bitrate to be allocated to that
  220. // observer in relation to other observers. For example with two observers, if
  221. // observer 1 had a bitrate_priority = 1.0, and observer 2 has a
  222. // bitrate_priority = 2.0, the expected behavior is that observer 2 will be
  223. // allocated twice the bitrate as observer 1 above the each observer's
  224. // min_bitrate_bps values, until one of the observers hits its max_bitrate_bps.
  225. std::map<BitrateAllocatorObserver*, int> NormalRateAllocation(
  226. const std::vector<AllocatableTrack>& allocatable_tracks,
  227. uint32_t bitrate,
  228. uint32_t sum_min_bitrates) {
  229. std::map<BitrateAllocatorObserver*, int> allocation;
  230. std::map<BitrateAllocatorObserver*, int> observers_capacities;
  231. for (const auto& observer_config : allocatable_tracks) {
  232. allocation[observer_config.observer] =
  233. observer_config.config.min_bitrate_bps;
  234. observers_capacities[observer_config.observer] =
  235. observer_config.config.max_bitrate_bps -
  236. observer_config.config.min_bitrate_bps;
  237. }
  238. bitrate -= sum_min_bitrates;
  239. // TODO(srte): Implement fair sharing between prioritized streams, currently
  240. // they are treated on a first come first serve basis.
  241. for (const auto& observer_config : allocatable_tracks) {
  242. int64_t priority_margin = observer_config.config.priority_bitrate_bps -
  243. allocation[observer_config.observer];
  244. if (priority_margin > 0 && bitrate > 0) {
  245. int64_t extra_bitrate = std::min<int64_t>(priority_margin, bitrate);
  246. allocation[observer_config.observer] +=
  247. rtc::dchecked_cast<int>(extra_bitrate);
  248. observers_capacities[observer_config.observer] -= extra_bitrate;
  249. bitrate -= extra_bitrate;
  250. }
  251. }
  252. // From the remaining bitrate, allocate a proportional amount to each observer
  253. // above the min bitrate already allocated.
  254. if (bitrate > 0)
  255. DistributeBitrateRelatively(allocatable_tracks, bitrate,
  256. observers_capacities, &allocation);
  257. return allocation;
  258. }
  259. // Allocates bitrate to observers when there is enough available bandwidth
  260. // for all observers to be allocated their max bitrate.
  261. std::map<BitrateAllocatorObserver*, int> MaxRateAllocation(
  262. const std::vector<AllocatableTrack>& allocatable_tracks,
  263. uint32_t bitrate,
  264. uint32_t sum_max_bitrates) {
  265. std::map<BitrateAllocatorObserver*, int> allocation;
  266. for (const auto& observer_config : allocatable_tracks) {
  267. allocation[observer_config.observer] =
  268. observer_config.config.max_bitrate_bps;
  269. bitrate -= observer_config.config.max_bitrate_bps;
  270. }
  271. DistributeBitrateEvenly(allocatable_tracks, bitrate, true,
  272. kTransmissionMaxBitrateMultiplier, &allocation);
  273. return allocation;
  274. }
  275. // Allocates zero bitrate to all observers.
  276. std::map<BitrateAllocatorObserver*, int> ZeroRateAllocation(
  277. const std::vector<AllocatableTrack>& allocatable_tracks) {
  278. std::map<BitrateAllocatorObserver*, int> allocation;
  279. for (const auto& observer_config : allocatable_tracks)
  280. allocation[observer_config.observer] = 0;
  281. return allocation;
  282. }
  283. std::map<BitrateAllocatorObserver*, int> AllocateBitrates(
  284. const std::vector<AllocatableTrack>& allocatable_tracks,
  285. uint32_t bitrate) {
  286. if (allocatable_tracks.empty())
  287. return std::map<BitrateAllocatorObserver*, int>();
  288. if (bitrate == 0)
  289. return ZeroRateAllocation(allocatable_tracks);
  290. uint32_t sum_min_bitrates = 0;
  291. uint32_t sum_max_bitrates = 0;
  292. for (const auto& observer_config : allocatable_tracks) {
  293. sum_min_bitrates += observer_config.config.min_bitrate_bps;
  294. sum_max_bitrates += observer_config.config.max_bitrate_bps;
  295. }
  296. // Not enough for all observers to get an allocation, allocate according to:
  297. // enforced min bitrate -> allocated bitrate previous round -> restart paused
  298. // streams.
  299. if (!EnoughBitrateForAllObservers(allocatable_tracks, bitrate,
  300. sum_min_bitrates))
  301. return LowRateAllocation(allocatable_tracks, bitrate);
  302. // All observers will get their min bitrate plus a share of the rest. This
  303. // share is allocated to each observer based on its bitrate_priority.
  304. if (bitrate <= sum_max_bitrates)
  305. return NormalRateAllocation(allocatable_tracks, bitrate, sum_min_bitrates);
  306. // All observers will get up to transmission_max_bitrate_multiplier_ x max.
  307. return MaxRateAllocation(allocatable_tracks, bitrate, sum_max_bitrates);
  308. }
  309. } // namespace
  310. BitrateAllocator::BitrateAllocator(LimitObserver* limit_observer)
  311. : limit_observer_(limit_observer),
  312. last_target_bps_(0),
  313. last_stable_target_bps_(0),
  314. last_non_zero_bitrate_bps_(kDefaultBitrateBps),
  315. last_fraction_loss_(0),
  316. last_rtt_(0),
  317. last_bwe_period_ms_(1000),
  318. num_pause_events_(0),
  319. last_bwe_log_time_(0) {
  320. sequenced_checker_.Detach();
  321. }
  322. BitrateAllocator::~BitrateAllocator() {
  323. RTC_HISTOGRAM_COUNTS_100("WebRTC.Call.NumberOfPauseEvents",
  324. num_pause_events_);
  325. }
  326. void BitrateAllocator::UpdateStartRate(uint32_t start_rate_bps) {
  327. RTC_DCHECK_RUN_ON(&sequenced_checker_);
  328. last_non_zero_bitrate_bps_ = start_rate_bps;
  329. }
  330. void BitrateAllocator::OnNetworkEstimateChanged(TargetTransferRate msg) {
  331. RTC_DCHECK_RUN_ON(&sequenced_checker_);
  332. last_target_bps_ = msg.target_rate.bps();
  333. last_stable_target_bps_ = msg.stable_target_rate.bps();
  334. last_non_zero_bitrate_bps_ =
  335. last_target_bps_ > 0 ? last_target_bps_ : last_non_zero_bitrate_bps_;
  336. int loss_ratio_255 = msg.network_estimate.loss_rate_ratio * 255;
  337. last_fraction_loss_ =
  338. rtc::dchecked_cast<uint8_t>(rtc::SafeClamp(loss_ratio_255, 0, 255));
  339. last_rtt_ = msg.network_estimate.round_trip_time.ms();
  340. last_bwe_period_ms_ = msg.network_estimate.bwe_period.ms();
  341. // Periodically log the incoming BWE.
  342. int64_t now = msg.at_time.ms();
  343. if (now > last_bwe_log_time_ + kBweLogIntervalMs) {
  344. RTC_LOG(LS_INFO) << "Current BWE " << last_target_bps_;
  345. last_bwe_log_time_ = now;
  346. }
  347. auto allocation = AllocateBitrates(allocatable_tracks_, last_target_bps_);
  348. auto stable_bitrate_allocation =
  349. AllocateBitrates(allocatable_tracks_, last_stable_target_bps_);
  350. for (auto& config : allocatable_tracks_) {
  351. uint32_t allocated_bitrate = allocation[config.observer];
  352. uint32_t allocated_stable_target_rate =
  353. stable_bitrate_allocation[config.observer];
  354. BitrateAllocationUpdate update;
  355. update.target_bitrate = DataRate::bps(allocated_bitrate);
  356. update.stable_target_bitrate = DataRate::bps(allocated_stable_target_rate);
  357. update.packet_loss_ratio = last_fraction_loss_ / 256.0;
  358. update.round_trip_time = TimeDelta::ms(last_rtt_);
  359. update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
  360. uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
  361. if (allocated_bitrate == 0 && config.allocated_bitrate_bps > 0) {
  362. if (last_target_bps_ > 0)
  363. ++num_pause_events_;
  364. // The protection bitrate is an estimate based on the ratio between media
  365. // and protection used before this observer was muted.
  366. uint32_t predicted_protection_bps =
  367. (1.0 - config.media_ratio) * config.config.min_bitrate_bps;
  368. RTC_LOG(LS_INFO) << "Pausing observer " << config.observer
  369. << " with configured min bitrate "
  370. << config.config.min_bitrate_bps
  371. << " and current estimate of " << last_target_bps_
  372. << " and protection bitrate "
  373. << predicted_protection_bps;
  374. } else if (allocated_bitrate > 0 && config.allocated_bitrate_bps == 0) {
  375. if (last_target_bps_ > 0)
  376. ++num_pause_events_;
  377. RTC_LOG(LS_INFO) << "Resuming observer " << config.observer
  378. << ", configured min bitrate "
  379. << config.config.min_bitrate_bps
  380. << ", current allocation " << allocated_bitrate
  381. << " and protection bitrate " << protection_bitrate;
  382. }
  383. // Only update the media ratio if the observer got an allocation.
  384. if (allocated_bitrate > 0)
  385. config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
  386. config.allocated_bitrate_bps = allocated_bitrate;
  387. }
  388. UpdateAllocationLimits();
  389. }
  390. void BitrateAllocator::AddObserver(BitrateAllocatorObserver* observer,
  391. MediaStreamAllocationConfig config) {
  392. RTC_DCHECK_RUN_ON(&sequenced_checker_);
  393. RTC_DCHECK_GT(config.bitrate_priority, 0);
  394. RTC_DCHECK(std::isnormal(config.bitrate_priority));
  395. auto it = absl::c_find_if(
  396. allocatable_tracks_,
  397. [observer](const auto& config) { return config.observer == observer; });
  398. // Update settings if the observer already exists, create a new one otherwise.
  399. if (it != allocatable_tracks_.end()) {
  400. it->config = config;
  401. } else {
  402. allocatable_tracks_.push_back(AllocatableTrack(observer, config));
  403. }
  404. if (last_target_bps_ > 0) {
  405. // Calculate a new allocation and update all observers.
  406. auto allocation = AllocateBitrates(allocatable_tracks_, last_target_bps_);
  407. auto stable_bitrate_allocation =
  408. AllocateBitrates(allocatable_tracks_, last_stable_target_bps_);
  409. for (auto& config : allocatable_tracks_) {
  410. uint32_t allocated_bitrate = allocation[config.observer];
  411. uint32_t allocated_stable_bitrate =
  412. stable_bitrate_allocation[config.observer];
  413. BitrateAllocationUpdate update;
  414. update.target_bitrate = DataRate::bps(allocated_bitrate);
  415. update.stable_target_bitrate = DataRate::bps(allocated_stable_bitrate);
  416. update.packet_loss_ratio = last_fraction_loss_ / 256.0;
  417. update.round_trip_time = TimeDelta::ms(last_rtt_);
  418. update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
  419. uint32_t protection_bitrate = config.observer->OnBitrateUpdated(update);
  420. config.allocated_bitrate_bps = allocated_bitrate;
  421. if (allocated_bitrate > 0)
  422. config.media_ratio = MediaRatio(allocated_bitrate, protection_bitrate);
  423. }
  424. } else {
  425. // Currently, an encoder is not allowed to produce frames.
  426. // But we still have to return the initial config bitrate + let the
  427. // observer know that it can not produce frames.
  428. BitrateAllocationUpdate update;
  429. update.target_bitrate = DataRate::Zero();
  430. update.stable_target_bitrate = DataRate::Zero();
  431. update.packet_loss_ratio = last_fraction_loss_ / 256.0;
  432. update.round_trip_time = TimeDelta::ms(last_rtt_);
  433. update.bwe_period = TimeDelta::ms(last_bwe_period_ms_);
  434. observer->OnBitrateUpdated(update);
  435. }
  436. UpdateAllocationLimits();
  437. }
  438. void BitrateAllocator::UpdateAllocationLimits() {
  439. BitrateAllocationLimits limits;
  440. for (const auto& config : allocatable_tracks_) {
  441. uint32_t stream_padding = config.config.pad_up_bitrate_bps;
  442. if (config.config.enforce_min_bitrate) {
  443. limits.min_allocatable_rate +=
  444. DataRate::bps(config.config.min_bitrate_bps);
  445. } else if (config.allocated_bitrate_bps == 0) {
  446. stream_padding =
  447. std::max(config.MinBitrateWithHysteresis(), stream_padding);
  448. }
  449. limits.max_padding_rate += DataRate::bps(stream_padding);
  450. limits.max_allocatable_rate += DataRate::bps(config.config.max_bitrate_bps);
  451. }
  452. if (limits.min_allocatable_rate == current_limits_.min_allocatable_rate &&
  453. limits.max_allocatable_rate == current_limits_.max_allocatable_rate &&
  454. limits.max_padding_rate == current_limits_.max_padding_rate) {
  455. return;
  456. }
  457. current_limits_ = limits;
  458. RTC_LOG(LS_INFO) << "UpdateAllocationLimits : total_requested_min_bitrate: "
  459. << ToString(limits.min_allocatable_rate)
  460. << ", total_requested_padding_bitrate: "
  461. << ToString(limits.max_padding_rate)
  462. << ", total_requested_max_bitrate: "
  463. << ToString(limits.max_allocatable_rate);
  464. limit_observer_->OnAllocationLimitsChanged(limits);
  465. }
  466. void BitrateAllocator::RemoveObserver(BitrateAllocatorObserver* observer) {
  467. RTC_DCHECK_RUN_ON(&sequenced_checker_);
  468. for (auto it = allocatable_tracks_.begin(); it != allocatable_tracks_.end();
  469. ++it) {
  470. if (it->observer == observer) {
  471. allocatable_tracks_.erase(it);
  472. break;
  473. }
  474. }
  475. UpdateAllocationLimits();
  476. }
  477. int BitrateAllocator::GetStartBitrate(
  478. BitrateAllocatorObserver* observer) const {
  479. RTC_DCHECK_RUN_ON(&sequenced_checker_);
  480. auto it = absl::c_find_if(
  481. allocatable_tracks_,
  482. [observer](const auto& config) { return config.observer == observer; });
  483. if (it == allocatable_tracks_.end()) {
  484. // This observer hasn't been added yet, just give it its fair share.
  485. return last_non_zero_bitrate_bps_ /
  486. static_cast<int>((allocatable_tracks_.size() + 1));
  487. } else if (it->allocated_bitrate_bps == -1) {
  488. // This observer hasn't received an allocation yet, so do the same.
  489. return last_non_zero_bitrate_bps_ /
  490. static_cast<int>(allocatable_tracks_.size());
  491. } else {
  492. // This observer already has an allocation.
  493. return it->allocated_bitrate_bps;
  494. }
  495. }
  496. uint32_t bitrate_allocator_impl::AllocatableTrack::LastAllocatedBitrate()
  497. const {
  498. // Return the configured minimum bitrate for newly added observers, to avoid
  499. // requiring an extra high bitrate for the observer to get an allocated
  500. // bitrate.
  501. return allocated_bitrate_bps == -1 ? config.min_bitrate_bps
  502. : allocated_bitrate_bps;
  503. }
  504. uint32_t bitrate_allocator_impl::AllocatableTrack::MinBitrateWithHysteresis()
  505. const {
  506. uint32_t min_bitrate = config.min_bitrate_bps;
  507. if (LastAllocatedBitrate() == 0) {
  508. min_bitrate += std::max(static_cast<uint32_t>(kToggleFactor * min_bitrate),
  509. kMinToggleBitrateBps);
  510. }
  511. // Account for protection bitrate used by this observer in the previous
  512. // allocation.
  513. // Note: the ratio will only be updated when the stream is active, meaning a
  514. // paused stream won't get any ratio updates. This might lead to waiting a bit
  515. // longer than necessary if the network condition improves, but this is to
  516. // avoid too much toggling.
  517. if (media_ratio > 0.0 && media_ratio < 1.0)
  518. min_bitrate += min_bitrate * (1.0 - media_ratio);
  519. return min_bitrate;
  520. }
  521. } // namespace webrtc