stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 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. |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef CALL_BITRATE_ALLOCATOR_H_ |
| 12 | #define CALL_BITRATE_ALLOCATOR_H_ |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 13 | |
kwiberg | b25345e | 2016-03-12 14:10:44 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 16 | #include <map> |
Alex Narest | 78609d5 | 2017-10-20 08:37:47 | [diff] [blame] | 17 | #include <memory> |
Alex Narest | b3944f0 | 2017-10-13 12:56:18 | [diff] [blame] | 18 | #include <string> |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 19 | #include <utility> |
mflodman | 48a4beb | 2016-07-01 11:03:59 | [diff] [blame] | 20 | #include <vector> |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 21 | |
Alex Narest | 78609d5 | 2017-10-20 08:37:47 | [diff] [blame] | 22 | #include "rtc_base/bitrateallocationstrategy.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 23 | #include "rtc_base/sequenced_task_checker.h" |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
mflodman | 48a4beb | 2016-07-01 11:03:59 | [diff] [blame] | 27 | class Clock; |
| 28 | |
mflodman | 86aabb2 | 2016-03-11 14:44:32 | [diff] [blame] | 29 | // Used by all send streams with adaptive bitrate, to get the currently |
| 30 | // allocated bitrate for the send stream. The current network properties are |
| 31 | // given at the same time, to let the send stream decide about possible loss |
| 32 | // protection. |
| 33 | class BitrateAllocatorObserver { |
| 34 | public: |
mflodman | 48a4beb | 2016-07-01 11:03:59 | [diff] [blame] | 35 | // Returns the amount of protection used by the BitrateAllocatorObserver |
| 36 | // implementation, as bitrate in bps. |
| 37 | virtual uint32_t OnBitrateUpdated(uint32_t bitrate_bps, |
| 38 | uint8_t fraction_loss, |
minyue | 78b4d56 | 2016-11-30 12:47:39 | [diff] [blame] | 39 | int64_t rtt, |
minyue | 93e4522 | 2017-05-18 21:32:41 | [diff] [blame] | 40 | int64_t bwe_period_ms) = 0; |
minyue | 78b4d56 | 2016-11-30 12:47:39 | [diff] [blame] | 41 | |
perkj | 71ee44c | 2016-06-15 07:47:53 | [diff] [blame] | 42 | protected: |
mflodman | 86aabb2 | 2016-03-11 14:44:32 | [diff] [blame] | 43 | virtual ~BitrateAllocatorObserver() {} |
| 44 | }; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 45 | |
mflodman | 86aabb2 | 2016-03-11 14:44:32 | [diff] [blame] | 46 | // Usage: this class will register multiple RtcpBitrateObserver's one at each |
| 47 | // RTCP module. It will aggregate the results and run one bandwidth estimation |
| 48 | // and push the result to the encoders via BitrateAllocatorObserver(s). |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 49 | class BitrateAllocator { |
| 50 | public: |
perkj | 71ee44c | 2016-06-15 07:47:53 | [diff] [blame] | 51 | // Used to get notified when send stream limits such as the minimum send |
| 52 | // bitrate and max padding bitrate is changed. |
| 53 | class LimitObserver { |
| 54 | public: |
| 55 | virtual void OnAllocationLimitsChanged( |
| 56 | uint32_t min_send_bitrate_bps, |
| 57 | uint32_t max_padding_bitrate_bps) = 0; |
| 58 | |
| 59 | protected: |
| 60 | virtual ~LimitObserver() {} |
| 61 | }; |
| 62 | |
| 63 | explicit BitrateAllocator(LimitObserver* limit_observer); |
mflodman | 48a4beb | 2016-07-01 11:03:59 | [diff] [blame] | 64 | ~BitrateAllocator(); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 65 | |
mflodman | 86aabb2 | 2016-03-11 14:44:32 | [diff] [blame] | 66 | // Allocate target_bitrate across the registered BitrateAllocatorObservers. |
perkj | 71ee44c | 2016-06-15 07:47:53 | [diff] [blame] | 67 | void OnNetworkChanged(uint32_t target_bitrate_bps, |
| 68 | uint8_t fraction_loss, |
minyue | 78b4d56 | 2016-11-30 12:47:39 | [diff] [blame] | 69 | int64_t rtt, |
minyue | 93e4522 | 2017-05-18 21:32:41 | [diff] [blame] | 70 | int64_t bwe_period_ms); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 71 | |
| 72 | // Set the start and max send bitrate used by the bandwidth management. |
| 73 | // |
Peter Boström | 8e4e8b0 | 2015-09-15 13:08:03 | [diff] [blame] | 74 | // |observer| updates bitrates if already in use. |
| 75 | // |min_bitrate_bps| = 0 equals no min bitrate. |
| 76 | // |max_bitrate_bps| = 0 equals no max bitrate. |
mflodman | 2ebe5b1 | 2016-05-13 08:43:51 | [diff] [blame] | 77 | // |enforce_min_bitrate| = 'true' will allocate at least |min_bitrate_bps| for |
| 78 | // this observer, even if the BWE is too low, 'false' will allocate 0 to |
| 79 | // the observer if BWE doesn't allow |min_bitrate_bps|. |
perkj | fea9309 | 2016-05-14 07:58:48 | [diff] [blame] | 80 | // Note that |observer|->OnBitrateUpdated() will be called within the scope of |
| 81 | // this method with the current rtt, fraction_loss and available bitrate and |
| 82 | // that the bitrate in OnBitrateUpdated will be zero if the |observer| is |
| 83 | // currently not allowed to send data. |
perkj | 57c21f9 | 2016-06-17 14:27:16 | [diff] [blame] | 84 | void AddObserver(BitrateAllocatorObserver* observer, |
| 85 | uint32_t min_bitrate_bps, |
| 86 | uint32_t max_bitrate_bps, |
| 87 | uint32_t pad_up_bitrate_bps, |
Alex Narest | b3944f0 | 2017-10-13 12:56:18 | [diff] [blame] | 88 | bool enforce_min_bitrate, |
Seth Hampson | fe73d6a | 2017-11-14 18:49:06 | [diff] [blame] | 89 | std::string track_id, |
| 90 | // TODO(shampson): Take out default value and wire the |
| 91 | // bitrate_priority up to the AudioSendStream::Config and |
| 92 | // VideoSendStream::Config. |
| 93 | double bitrate_priority = 1.0); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 94 | |
mflodman | 101f250 | 2016-06-09 15:21:19 | [diff] [blame] | 95 | // Removes a previously added observer, but will not trigger a new bitrate |
| 96 | // allocation. |
mflodman | 86aabb2 | 2016-03-11 14:44:32 | [diff] [blame] | 97 | void RemoveObserver(BitrateAllocatorObserver* observer); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 98 | |
perkj | 57c21f9 | 2016-06-17 14:27:16 | [diff] [blame] | 99 | // Returns initial bitrate allocated for |observer|. If |observer| is not in |
| 100 | // the list of added observers, a best guess is returned. |
| 101 | int GetStartBitrate(BitrateAllocatorObserver* observer); |
| 102 | |
Alex Narest | 78609d5 | 2017-10-20 08:37:47 | [diff] [blame] | 103 | // Sets external allocation strategy. If strategy is not set default WebRTC |
| 104 | // allocation mechanism will be used. The strategy may be changed during call. |
| 105 | // Setting NULL value will restore default WEBRTC allocation strategy. |
| 106 | void SetBitrateAllocationStrategy( |
| 107 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 108 | bitrate_allocation_strategy); |
| 109 | |
mflodman | 2ebe5b1 | 2016-05-13 08:43:51 | [diff] [blame] | 110 | private: |
Alex Narest | 78609d5 | 2017-10-20 08:37:47 | [diff] [blame] | 111 | struct ObserverConfig : rtc::BitrateAllocationStrategy::TrackConfig { |
mflodman | 2ebe5b1 | 2016-05-13 08:43:51 | [diff] [blame] | 112 | ObserverConfig(BitrateAllocatorObserver* observer, |
| 113 | uint32_t min_bitrate_bps, |
| 114 | uint32_t max_bitrate_bps, |
perkj | 71ee44c | 2016-06-15 07:47:53 | [diff] [blame] | 115 | uint32_t pad_up_bitrate_bps, |
Alex Narest | b3944f0 | 2017-10-13 12:56:18 | [diff] [blame] | 116 | bool enforce_min_bitrate, |
Seth Hampson | fe73d6a | 2017-11-14 18:49:06 | [diff] [blame] | 117 | std::string track_id, |
| 118 | double bitrate_priority) |
Alex Narest | 78609d5 | 2017-10-20 08:37:47 | [diff] [blame] | 119 | : TrackConfig(min_bitrate_bps, |
| 120 | max_bitrate_bps, |
| 121 | enforce_min_bitrate, |
| 122 | track_id), |
| 123 | observer(observer), |
perkj | 71ee44c | 2016-06-15 07:47:53 | [diff] [blame] | 124 | pad_up_bitrate_bps(pad_up_bitrate_bps), |
mflodman | 48a4beb | 2016-07-01 11:03:59 | [diff] [blame] | 125 | allocated_bitrate_bps(-1), |
Seth Hampson | fe73d6a | 2017-11-14 18:49:06 | [diff] [blame] | 126 | media_ratio(1.0), |
| 127 | bitrate_priority(bitrate_priority) {} |
mflodman | 48a4beb | 2016-07-01 11:03:59 | [diff] [blame] | 128 | |
| 129 | BitrateAllocatorObserver* observer; |
perkj | 71ee44c | 2016-06-15 07:47:53 | [diff] [blame] | 130 | uint32_t pad_up_bitrate_bps; |
mflodman | 48a4beb | 2016-07-01 11:03:59 | [diff] [blame] | 131 | int64_t allocated_bitrate_bps; |
| 132 | double media_ratio; // Part of the total bitrate used for media [0.0, 1.0]. |
Seth Hampson | fe73d6a | 2017-11-14 18:49:06 | [diff] [blame] | 133 | // The amount of bitrate allocated to this observer relative to all other |
| 134 | // observers. If an observer has twice the bitrate_priority of other |
| 135 | // observers, it should be allocated twice the bitrate above its min. |
| 136 | double bitrate_priority; |
mflodman | 2ebe5b1 | 2016-05-13 08:43:51 | [diff] [blame] | 137 | }; |
| 138 | |
perkj | 71ee44c | 2016-06-15 07:47:53 | [diff] [blame] | 139 | // Calculates the minimum requested send bitrate and max padding bitrate and |
| 140 | // calls LimitObserver::OnAllocationLimitsChanged. |
| 141 | void UpdateAllocationLimits(); |
| 142 | |
mflodman | 48a4beb | 2016-07-01 11:03:59 | [diff] [blame] | 143 | typedef std::vector<ObserverConfig> ObserverConfigs; |
| 144 | ObserverConfigs::iterator FindObserverConfig( |
perkj | 26091b1 | 2016-09-01 08:17:40 | [diff] [blame] | 145 | const BitrateAllocatorObserver* observer); |
mflodman | 2ebe5b1 | 2016-05-13 08:43:51 | [diff] [blame] | 146 | |
| 147 | typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap; |
| 148 | typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation; |
| 149 | |
perkj | 26091b1 | 2016-09-01 08:17:40 | [diff] [blame] | 150 | ObserverAllocation AllocateBitrates(uint32_t bitrate); |
Stefan Holmer | e590416 | 2015-03-26 10:11:06 | [diff] [blame] | 151 | |
Seth Hampson | fe73d6a | 2017-11-14 18:49:06 | [diff] [blame] | 152 | // Allocates zero bitrate to all observers. |
perkj | 26091b1 | 2016-09-01 08:17:40 | [diff] [blame] | 153 | ObserverAllocation ZeroRateAllocation(); |
Seth Hampson | fe73d6a | 2017-11-14 18:49:06 | [diff] [blame] | 154 | // Allocates bitrate to observers when there isn't enough to allocate the |
| 155 | // minimum to all observers. |
perkj | 26091b1 | 2016-09-01 08:17:40 | [diff] [blame] | 156 | ObserverAllocation LowRateAllocation(uint32_t bitrate); |
Seth Hampson | fe73d6a | 2017-11-14 18:49:06 | [diff] [blame] | 157 | // Allocates bitrate to all observers when the available bandwidth is enough |
| 158 | // to allocate the minimum to all observers but not enough to allocate the |
| 159 | // max bitrate of each observer. |
mflodman | 101f250 | 2016-06-09 15:21:19 | [diff] [blame] | 160 | ObserverAllocation NormalRateAllocation(uint32_t bitrate, |
perkj | 26091b1 | 2016-09-01 08:17:40 | [diff] [blame] | 161 | uint32_t sum_min_bitrates); |
Seth Hampson | fe73d6a | 2017-11-14 18:49:06 | [diff] [blame] | 162 | // Allocates bitrate to observers when there is enough available bandwidth |
| 163 | // for all observers to be allocated their max bitrate. |
mflodman | 101f250 | 2016-06-09 15:21:19 | [diff] [blame] | 164 | ObserverAllocation MaxRateAllocation(uint32_t bitrate, |
perkj | 26091b1 | 2016-09-01 08:17:40 | [diff] [blame] | 165 | uint32_t sum_max_bitrates); |
mflodman | 101f250 | 2016-06-09 15:21:19 | [diff] [blame] | 166 | |
perkj | 26091b1 | 2016-09-01 08:17:40 | [diff] [blame] | 167 | uint32_t LastAllocatedBitrate(const ObserverConfig& observer_config); |
mflodman | 101f250 | 2016-06-09 15:21:19 | [diff] [blame] | 168 | // The minimum bitrate required by this observer, including enable-hysteresis |
| 169 | // if the observer is in a paused state. |
perkj | 26091b1 | 2016-09-01 08:17:40 | [diff] [blame] | 170 | uint32_t MinBitrateWithHysteresis(const ObserverConfig& observer_config); |
mflodman | 101f250 | 2016-06-09 15:21:19 | [diff] [blame] | 171 | // Splits |bitrate| evenly to observers already in |allocation|. |
| 172 | // |include_zero_allocations| decides if zero allocations should be part of |
| 173 | // the distribution or not. The allowed max bitrate is |max_multiplier| x |
| 174 | // observer max bitrate. |
| 175 | void DistributeBitrateEvenly(uint32_t bitrate, |
| 176 | bool include_zero_allocations, |
| 177 | int max_multiplier, |
perkj | 26091b1 | 2016-09-01 08:17:40 | [diff] [blame] | 178 | ObserverAllocation* allocation); |
| 179 | bool EnoughBitrateForAllObservers(uint32_t bitrate, |
| 180 | uint32_t sum_min_bitrates); |
mflodman | 101f250 | 2016-06-09 15:21:19 | [diff] [blame] | 181 | |
Seth Hampson | fe73d6a | 2017-11-14 18:49:06 | [diff] [blame] | 182 | // From the available |bitrate|, each observer will be allocated a |
| 183 | // proportional amount based upon its bitrate priority. If that amount is |
| 184 | // more than the observer's capacity, it will be allocated its capacity, and |
| 185 | // the excess bitrate is still allocated proportionally to other observers. |
| 186 | // Allocating the proportional amount means an observer with twice the |
| 187 | // bitrate_priority of another will be allocated twice the bitrate. |
| 188 | void DistributeBitrateRelatively( |
| 189 | uint32_t bitrate, |
| 190 | const ObserverAllocation& observers_capacities, |
| 191 | ObserverAllocation* allocation); |
| 192 | |
perkj | 26091b1 | 2016-09-01 08:17:40 | [diff] [blame] | 193 | rtc::SequencedTaskChecker sequenced_checker_; |
danilchap | a37de39 | 2017-09-09 11:17:22 | [diff] [blame] | 194 | LimitObserver* const limit_observer_ RTC_GUARDED_BY(&sequenced_checker_); |
Stefan Holmer | e590416 | 2015-03-26 10:11:06 | [diff] [blame] | 195 | // Stored in a list to keep track of the insertion order. |
danilchap | a37de39 | 2017-09-09 11:17:22 | [diff] [blame] | 196 | ObserverConfigs bitrate_observer_configs_ RTC_GUARDED_BY(&sequenced_checker_); |
| 197 | uint32_t last_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_); |
| 198 | uint32_t last_non_zero_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_); |
| 199 | uint8_t last_fraction_loss_ RTC_GUARDED_BY(&sequenced_checker_); |
| 200 | int64_t last_rtt_ RTC_GUARDED_BY(&sequenced_checker_); |
| 201 | int64_t last_bwe_period_ms_ RTC_GUARDED_BY(&sequenced_checker_); |
mflodman | 48a4beb | 2016-07-01 11:03:59 | [diff] [blame] | 202 | // Number of mute events based on too low BWE, not network up/down. |
danilchap | a37de39 | 2017-09-09 11:17:22 | [diff] [blame] | 203 | int num_pause_events_ RTC_GUARDED_BY(&sequenced_checker_); |
| 204 | Clock* const clock_ RTC_GUARDED_BY(&sequenced_checker_); |
| 205 | int64_t last_bwe_log_time_ RTC_GUARDED_BY(&sequenced_checker_); |
| 206 | uint32_t total_requested_padding_bitrate_ RTC_GUARDED_BY(&sequenced_checker_); |
| 207 | uint32_t total_requested_min_bitrate_ RTC_GUARDED_BY(&sequenced_checker_); |
Alex Narest | 78609d5 | 2017-10-20 08:37:47 | [diff] [blame] | 208 | std::unique_ptr<rtc::BitrateAllocationStrategy> bitrate_allocation_strategy_ |
| 209 | RTC_GUARDED_BY(&sequenced_checker_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 210 | }; |
Seth Hampson | fe73d6a | 2017-11-14 18:49:06 | [diff] [blame] | 211 | |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 212 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 213 | #endif // CALL_BITRATE_ALLOCATOR_H_ |