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