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 | |
Sebastian Jansson | 6736df1 | 2018-11-21 18:18:39 | [diff] [blame] | 22 | #include "api/call/bitrate_allocation.h" |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 23 | #include "api/sequence_checker.h" |
Sebastian Jansson | 93b1ea2 | 2019-09-18 16:31:52 | [diff] [blame] | 24 | #include "api/transport/network_types.h" |
Mirko Bonadei | 20e4c80 | 2020-11-23 10:07:42 | [diff] [blame] | 25 | #include "rtc_base/system/no_unique_address.h" |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
mflodman | 48a4beb | 2016-07-01 11:03:59 | [diff] [blame] | 29 | class Clock; |
| 30 | |
mflodman | 86aabb2 | 2016-03-11 14:44:32 | [diff] [blame] | 31 | // Used by all send streams with adaptive bitrate, to get the currently |
| 32 | // allocated bitrate for the send stream. The current network properties are |
| 33 | // given at the same time, to let the send stream decide about possible loss |
| 34 | // protection. |
| 35 | class BitrateAllocatorObserver { |
| 36 | public: |
mflodman | 48a4beb | 2016-07-01 11:03:59 | [diff] [blame] | 37 | // Returns the amount of protection used by the BitrateAllocatorObserver |
| 38 | // implementation, as bitrate in bps. |
Sebastian Jansson | c0e4d45 | 2018-10-25 13:08:32 | [diff] [blame] | 39 | virtual uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) = 0; |
minyue | 78b4d56 | 2016-11-30 12:47:39 | [diff] [blame] | 40 | |
perkj | 71ee44c | 2016-06-15 07:47:53 | [diff] [blame] | 41 | protected: |
mflodman | 86aabb2 | 2016-03-11 14:44:32 | [diff] [blame] | 42 | virtual ~BitrateAllocatorObserver() {} |
| 43 | }; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 44 | |
Sebastian Jansson | 24ad720 | 2018-04-19 06:25:12 | [diff] [blame] | 45 | // Struct describing parameters for how a media stream should get bitrate |
Sebastian Jansson | 4d461ba | 2019-09-17 18:53:26 | [diff] [blame] | 46 | // allocated to it. |
| 47 | |
Sebastian Jansson | 24ad720 | 2018-04-19 06:25:12 | [diff] [blame] | 48 | struct MediaStreamAllocationConfig { |
Sebastian Jansson | 4d461ba | 2019-09-17 18:53:26 | [diff] [blame] | 49 | // Minimum bitrate supported by track. 0 equals no min bitrate. |
Sebastian Jansson | 24ad720 | 2018-04-19 06:25:12 | [diff] [blame] | 50 | uint32_t min_bitrate_bps; |
Sebastian Jansson | 4d461ba | 2019-09-17 18:53:26 | [diff] [blame] | 51 | // Maximum bitrate supported by track. 0 equals no max bitrate. |
Sebastian Jansson | 24ad720 | 2018-04-19 06:25:12 | [diff] [blame] | 52 | uint32_t max_bitrate_bps; |
| 53 | uint32_t pad_up_bitrate_bps; |
Sebastian Jansson | 464a557 | 2019-02-12 12:32:32 | [diff] [blame] | 54 | int64_t priority_bitrate_bps; |
Sebastian Jansson | 4d461ba | 2019-09-17 18:53:26 | [diff] [blame] | 55 | // True means track may not be paused by allocating 0 bitrate will allocate at |
Artem Titov | ea24027 | 2021-07-26 10:40:21 | [diff] [blame] | 56 | // least `min_bitrate_bps` for this observer, even if the BWE is too low, |
Sebastian Jansson | 4d461ba | 2019-09-17 18:53:26 | [diff] [blame] | 57 | // false will allocate 0 to the observer if BWE doesn't allow |
Artem Titov | ea24027 | 2021-07-26 10:40:21 | [diff] [blame] | 58 | // `min_bitrate_bps`. |
Sebastian Jansson | 24ad720 | 2018-04-19 06:25:12 | [diff] [blame] | 59 | bool enforce_min_bitrate; |
Sebastian Jansson | 4d461ba | 2019-09-17 18:53:26 | [diff] [blame] | 60 | // The amount of bitrate allocated to this observer relative to all other |
| 61 | // observers. If an observer has twice the bitrate_priority of other |
| 62 | // observers, it should be allocated twice the bitrate above its min. |
Sebastian Jansson | 24ad720 | 2018-04-19 06:25:12 | [diff] [blame] | 63 | double bitrate_priority; |
Sebastian Jansson | 24ad720 | 2018-04-19 06:25:12 | [diff] [blame] | 64 | }; |
| 65 | |
Sebastian Jansson | 8326780 | 2018-04-19 06:27:19 | [diff] [blame] | 66 | // Interface used for mocking |
| 67 | class BitrateAllocatorInterface { |
| 68 | public: |
| 69 | virtual void AddObserver(BitrateAllocatorObserver* observer, |
| 70 | MediaStreamAllocationConfig config) = 0; |
| 71 | virtual void RemoveObserver(BitrateAllocatorObserver* observer) = 0; |
Sebastian Jansson | 44a262a | 2018-10-24 14:07:20 | [diff] [blame] | 72 | virtual int GetStartBitrate(BitrateAllocatorObserver* observer) const = 0; |
Sebastian Jansson | 8326780 | 2018-04-19 06:27:19 | [diff] [blame] | 73 | |
| 74 | protected: |
| 75 | virtual ~BitrateAllocatorInterface() = default; |
| 76 | }; |
| 77 | |
Sebastian Jansson | 538ca57 | 2019-09-24 17:47:26 | [diff] [blame] | 78 | namespace bitrate_allocator_impl { |
| 79 | struct AllocatableTrack { |
| 80 | AllocatableTrack(BitrateAllocatorObserver* observer, |
| 81 | MediaStreamAllocationConfig allocation_config) |
| 82 | : observer(observer), |
| 83 | config(allocation_config), |
| 84 | allocated_bitrate_bps(-1), |
| 85 | media_ratio(1.0) {} |
| 86 | BitrateAllocatorObserver* observer; |
| 87 | MediaStreamAllocationConfig config; |
| 88 | int64_t allocated_bitrate_bps; |
| 89 | double media_ratio; // Part of the total bitrate used for media [0.0, 1.0]. |
| 90 | |
| 91 | uint32_t LastAllocatedBitrate() const; |
| 92 | // The minimum bitrate required by this observer, including |
| 93 | // enable-hysteresis if the observer is in a paused state. |
| 94 | uint32_t MinBitrateWithHysteresis() const; |
| 95 | }; |
| 96 | } // namespace bitrate_allocator_impl |
| 97 | |
mflodman | 86aabb2 | 2016-03-11 14:44:32 | [diff] [blame] | 98 | // Usage: this class will register multiple RtcpBitrateObserver's one at each |
| 99 | // RTCP module. It will aggregate the results and run one bandwidth estimation |
| 100 | // and push the result to the encoders via BitrateAllocatorObserver(s). |
Sebastian Jansson | 8326780 | 2018-04-19 06:27:19 | [diff] [blame] | 101 | class BitrateAllocator : public BitrateAllocatorInterface { |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 102 | public: |
perkj | 71ee44c | 2016-06-15 07:47:53 | [diff] [blame] | 103 | // Used to get notified when send stream limits such as the minimum send |
| 104 | // bitrate and max padding bitrate is changed. |
| 105 | class LimitObserver { |
| 106 | public: |
Sebastian Jansson | 93b1ea2 | 2019-09-18 16:31:52 | [diff] [blame] | 107 | virtual void OnAllocationLimitsChanged(BitrateAllocationLimits limits) = 0; |
perkj | 71ee44c | 2016-06-15 07:47:53 | [diff] [blame] | 108 | |
| 109 | protected: |
Sebastian Jansson | 8326780 | 2018-04-19 06:27:19 | [diff] [blame] | 110 | virtual ~LimitObserver() = default; |
perkj | 71ee44c | 2016-06-15 07:47:53 | [diff] [blame] | 111 | }; |
| 112 | |
Sebastian Jansson | 40de3cc | 2019-09-19 12:54:43 | [diff] [blame] | 113 | explicit BitrateAllocator(LimitObserver* limit_observer); |
Stefan Holmer | dbdb3a0 | 2018-07-17 14:03:46 | [diff] [blame] | 114 | ~BitrateAllocator() override; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 115 | |
Sebastian Jansson | 2701bc9 | 2018-12-11 14:02:47 | [diff] [blame] | 116 | void UpdateStartRate(uint32_t start_rate_bps); |
| 117 | |
mflodman | 86aabb2 | 2016-03-11 14:44:32 | [diff] [blame] | 118 | // Allocate target_bitrate across the registered BitrateAllocatorObservers. |
Sebastian Jansson | 40de3cc | 2019-09-19 12:54:43 | [diff] [blame] | 119 | void OnNetworkEstimateChanged(TargetTransferRate msg); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 120 | |
Sebastian Jansson | 29b204e | 2018-03-21 11:45:27 | [diff] [blame] | 121 | // Set the configuration used by the bandwidth management. |
Artem Titov | ea24027 | 2021-07-26 10:40:21 | [diff] [blame] | 122 | // `observer` updates bitrates if already in use. |
| 123 | // `config` is the configuration to use for allocation. |
| 124 | // Note that `observer`->OnBitrateUpdated() will be called |
Sebastian Jansson | 4d461ba | 2019-09-17 18:53:26 | [diff] [blame] | 125 | // within the scope of this method with the current rtt, fraction_loss and |
| 126 | // available bitrate and that the bitrate in OnBitrateUpdated will be zero if |
Artem Titov | ea24027 | 2021-07-26 10:40:21 | [diff] [blame] | 127 | // the `observer` is currently not allowed to send data. |
perkj | 57c21f9 | 2016-06-17 14:27:16 | [diff] [blame] | 128 | void AddObserver(BitrateAllocatorObserver* observer, |
Sebastian Jansson | 8326780 | 2018-04-19 06:27:19 | [diff] [blame] | 129 | MediaStreamAllocationConfig config) override; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 130 | |
mflodman | 101f250 | 2016-06-09 15:21:19 | [diff] [blame] | 131 | // Removes a previously added observer, but will not trigger a new bitrate |
| 132 | // allocation. |
Sebastian Jansson | 8326780 | 2018-04-19 06:27:19 | [diff] [blame] | 133 | void RemoveObserver(BitrateAllocatorObserver* observer) override; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 134 | |
Artem Titov | ea24027 | 2021-07-26 10:40:21 | [diff] [blame] | 135 | // Returns initial bitrate allocated for `observer`. If `observer` is not in |
perkj | 57c21f9 | 2016-06-17 14:27:16 | [diff] [blame] | 136 | // the list of added observers, a best guess is returned. |
Sebastian Jansson | 44a262a | 2018-10-24 14:07:20 | [diff] [blame] | 137 | int GetStartBitrate(BitrateAllocatorObserver* observer) const override; |
perkj | 57c21f9 | 2016-06-17 14:27:16 | [diff] [blame] | 138 | |
mflodman | 2ebe5b1 | 2016-05-13 08:43:51 | [diff] [blame] | 139 | private: |
Sebastian Jansson | 538ca57 | 2019-09-24 17:47:26 | [diff] [blame] | 140 | using AllocatableTrack = bitrate_allocator_impl::AllocatableTrack; |
mflodman | 2ebe5b1 | 2016-05-13 08:43:51 | [diff] [blame] | 141 | |
perkj | 71ee44c | 2016-06-15 07:47:53 | [diff] [blame] | 142 | // Calculates the minimum requested send bitrate and max padding bitrate and |
| 143 | // calls LimitObserver::OnAllocationLimitsChanged. |
Niels Möller | d4043f6 | 2018-04-26 14:06:22 | [diff] [blame] | 144 | void UpdateAllocationLimits() RTC_RUN_ON(&sequenced_checker_); |
perkj | 71ee44c | 2016-06-15 07:47:53 | [diff] [blame] | 145 | |
Ying Wang | a646d30 | 2018-03-02 16:04:11 | [diff] [blame] | 146 | // Allow packets to be transmitted in up to 2 times max video bitrate if the |
| 147 | // bandwidth estimate allows it. |
| 148 | // TODO(bugs.webrtc.org/8541): May be worth to refactor to keep this logic in |
Jonas Olsson | 0182a03 | 2019-07-09 10:31:20 | [diff] [blame] | 149 | // video send stream. |
Niels Möller | 74e5f80 | 2018-04-25 12:03:46 | [diff] [blame] | 150 | static uint8_t GetTransmissionMaxBitrateMultiplier(); |
Ying Wang | a646d30 | 2018-03-02 16:04:11 | [diff] [blame] | 151 | |
Mirko Bonadei | 20e4c80 | 2020-11-23 10:07:42 | [diff] [blame] | 152 | RTC_NO_UNIQUE_ADDRESS SequenceChecker sequenced_checker_; |
danilchap | a37de39 | 2017-09-09 11:17:22 | [diff] [blame] | 153 | LimitObserver* const limit_observer_ RTC_GUARDED_BY(&sequenced_checker_); |
Stefan Holmer | e590416 | 2015-03-26 10:11:06 | [diff] [blame] | 154 | // Stored in a list to keep track of the insertion order. |
Sebastian Jansson | 4d461ba | 2019-09-17 18:53:26 | [diff] [blame] | 155 | std::vector<AllocatableTrack> allocatable_tracks_ |
| 156 | RTC_GUARDED_BY(&sequenced_checker_); |
Sebastian Jansson | 89c94b9 | 2018-11-20 16:16:36 | [diff] [blame] | 157 | uint32_t last_target_bps_ RTC_GUARDED_BY(&sequenced_checker_); |
Florent Castelli | 4e615d5 | 2019-08-22 14:09:06 | [diff] [blame] | 158 | uint32_t last_stable_target_bps_ RTC_GUARDED_BY(&sequenced_checker_); |
danilchap | a37de39 | 2017-09-09 11:17:22 | [diff] [blame] | 159 | uint32_t last_non_zero_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_); |
| 160 | uint8_t last_fraction_loss_ RTC_GUARDED_BY(&sequenced_checker_); |
| 161 | int64_t last_rtt_ RTC_GUARDED_BY(&sequenced_checker_); |
| 162 | int64_t last_bwe_period_ms_ RTC_GUARDED_BY(&sequenced_checker_); |
mflodman | 48a4beb | 2016-07-01 11:03:59 | [diff] [blame] | 163 | // Number of mute events based on too low BWE, not network up/down. |
danilchap | a37de39 | 2017-09-09 11:17:22 | [diff] [blame] | 164 | int num_pause_events_ RTC_GUARDED_BY(&sequenced_checker_); |
danilchap | a37de39 | 2017-09-09 11:17:22 | [diff] [blame] | 165 | int64_t last_bwe_log_time_ RTC_GUARDED_BY(&sequenced_checker_); |
Sebastian Jansson | 93b1ea2 | 2019-09-18 16:31:52 | [diff] [blame] | 166 | BitrateAllocationLimits current_limits_ RTC_GUARDED_BY(&sequenced_checker_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 167 | }; |
Seth Hampson | fe73d6a | 2017-11-14 18:49:06 | [diff] [blame] | 168 | |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 | [diff] [blame] | 169 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 170 | #endif // CALL_BITRATE_ALLOCATOR_H_ |