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