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