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