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