blob: 1323aa3288dafeade0ff49effa26d8671c4e3132 [file] [log] [blame]
stefan@webrtc.org792f1a12015-03-04 12:24:26 +00001/*
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.org792f1a12015-03-04 12:24:26 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef CALL_BITRATE_ALLOCATOR_H_
12#define CALL_BITRATE_ALLOCATOR_H_
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000013
kwibergb25345e2016-03-12 06:10:44 -080014#include <stdint.h>
15
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000016#include <map>
Alex Narest78609d52017-10-20 10:37:47 +020017#include <memory>
Alex Narestb3944f02017-10-13 14:56:18 +020018#include <string>
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000019#include <utility>
mflodman48a4beb2016-07-01 13:03:59 +020020#include <vector>
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000021
Sebastian Jansson6736df12018-11-21 19:18:39 +010022#include "api/call/bitrate_allocation.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/bitrate_allocation_strategy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/sequenced_task_checker.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000025
26namespace webrtc {
27
mflodman48a4beb2016-07-01 13:03:59 +020028class Clock;
29
mflodman86aabb22016-03-11 15:44:32 +010030// 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.
34class BitrateAllocatorObserver {
35 public:
mflodman48a4beb2016-07-01 13:03:59 +020036 // Returns the amount of protection used by the BitrateAllocatorObserver
37 // implementation, as bitrate in bps.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020038 virtual uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) = 0;
minyue78b4d562016-11-30 04:47:39 -080039
perkj71ee44c2016-06-15 00:47:53 -070040 protected:
mflodman86aabb22016-03-11 15:44:32 +010041 virtual ~BitrateAllocatorObserver() {}
42};
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000043
Sebastian Jansson24ad7202018-04-19 08:25:12 +020044// Struct describing parameters for how a media stream should get bitrate
45// allocated to it. |min_bitrate_bps| = 0 equals no min bitrate.
46// |max_bitrate_bps| = 0 equals no max bitrate.
47// |enforce_min_bitrate| = 'true' will allocate at least |min_bitrate_bps| for
48// this observer, even if the BWE is too low, 'false' will allocate 0 to
49// the observer if BWE doesn't allow |min_bitrate_bps|.
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +010050// Note that |observer|->OnBitrateUpdated() will be called
Sebastian Jansson24ad7202018-04-19 08:25:12 +020051// within the scope of this method with the current rtt, fraction_loss and
52// available bitrate and that the bitrate in OnBitrateUpdated will be zero if
53// the |observer| is currently not allowed to send data.
54struct MediaStreamAllocationConfig {
55 uint32_t min_bitrate_bps;
56 uint32_t max_bitrate_bps;
57 uint32_t pad_up_bitrate_bps;
58 bool enforce_min_bitrate;
59 std::string track_id;
60 double bitrate_priority;
Sebastian Jansson24ad7202018-04-19 08:25:12 +020061};
62
Sebastian Jansson83267802018-04-19 08:27:19 +020063// Interface used for mocking
64class BitrateAllocatorInterface {
65 public:
66 virtual void AddObserver(BitrateAllocatorObserver* observer,
67 MediaStreamAllocationConfig config) = 0;
68 virtual void RemoveObserver(BitrateAllocatorObserver* observer) = 0;
Sebastian Jansson44a262a2018-10-24 16:07:20 +020069 virtual int GetStartBitrate(BitrateAllocatorObserver* observer) const = 0;
Sebastian Jansson83267802018-04-19 08:27:19 +020070
71 protected:
72 virtual ~BitrateAllocatorInterface() = default;
73};
74
mflodman86aabb22016-03-11 15:44:32 +010075// Usage: this class will register multiple RtcpBitrateObserver's one at each
76// RTCP module. It will aggregate the results and run one bandwidth estimation
77// and push the result to the encoders via BitrateAllocatorObserver(s).
Sebastian Jansson83267802018-04-19 08:27:19 +020078class BitrateAllocator : public BitrateAllocatorInterface {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000079 public:
perkj71ee44c2016-06-15 00:47:53 -070080 // Used to get notified when send stream limits such as the minimum send
81 // bitrate and max padding bitrate is changed.
82 class LimitObserver {
83 public:
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +010084 virtual void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
85 uint32_t max_padding_bitrate_bps,
86 uint32_t total_bitrate_bps) = 0;
perkj71ee44c2016-06-15 00:47:53 -070087
88 protected:
Sebastian Jansson83267802018-04-19 08:27:19 +020089 virtual ~LimitObserver() = default;
perkj71ee44c2016-06-15 00:47:53 -070090 };
91
92 explicit BitrateAllocator(LimitObserver* limit_observer);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020093 ~BitrateAllocator() override;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000094
Sebastian Jansson2701bc92018-12-11 15:02:47 +010095 void UpdateStartRate(uint32_t start_rate_bps);
96
mflodman86aabb22016-03-11 15:44:32 +010097 // Allocate target_bitrate across the registered BitrateAllocatorObservers.
perkj71ee44c2016-06-15 00:47:53 -070098 void OnNetworkChanged(uint32_t target_bitrate_bps,
Sebastian Jansson89c94b92018-11-20 17:16:36 +010099 uint32_t link_capacity_bps,
perkj71ee44c2016-06-15 00:47:53 -0700100 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800101 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -0700102 int64_t bwe_period_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000103
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100104 // Set the configuration used by the bandwidth management.
Peter Boström8e4e8b02015-09-15 15:08:03 +0200105 // |observer| updates bitrates if already in use.
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200106 // |config| is the configuration to use for allocation.
perkj57c21f92016-06-17 07:27:16 -0700107 void AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson83267802018-04-19 08:27:19 +0200108 MediaStreamAllocationConfig config) override;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000109
mflodman101f2502016-06-09 17:21:19 +0200110 // Removes a previously added observer, but will not trigger a new bitrate
111 // allocation.
Sebastian Jansson83267802018-04-19 08:27:19 +0200112 void RemoveObserver(BitrateAllocatorObserver* observer) override;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000113
perkj57c21f92016-06-17 07:27:16 -0700114 // Returns initial bitrate allocated for |observer|. If |observer| is not in
115 // the list of added observers, a best guess is returned.
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200116 int GetStartBitrate(BitrateAllocatorObserver* observer) const override;
perkj57c21f92016-06-17 07:27:16 -0700117
Alex Narest78609d52017-10-20 10:37:47 +0200118 // Sets external allocation strategy. If strategy is not set default WebRTC
119 // allocation mechanism will be used. The strategy may be changed during call.
120 // Setting NULL value will restore default WEBRTC allocation strategy.
121 void SetBitrateAllocationStrategy(
122 std::unique_ptr<rtc::BitrateAllocationStrategy>
123 bitrate_allocation_strategy);
124
mflodman2ebe5b12016-05-13 01:43:51 -0700125 private:
Alex Narest78609d52017-10-20 10:37:47 +0200126 struct ObserverConfig : rtc::BitrateAllocationStrategy::TrackConfig {
mflodman2ebe5b12016-05-13 01:43:51 -0700127 ObserverConfig(BitrateAllocatorObserver* observer,
128 uint32_t min_bitrate_bps,
129 uint32_t max_bitrate_bps,
perkj71ee44c2016-06-15 00:47:53 -0700130 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200131 bool enforce_min_bitrate,
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800132 std::string track_id,
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100133 double bitrate_priority)
Alex Narest78609d52017-10-20 10:37:47 +0200134 : TrackConfig(min_bitrate_bps,
135 max_bitrate_bps,
136 enforce_min_bitrate,
137 track_id),
138 observer(observer),
perkj71ee44c2016-06-15 00:47:53 -0700139 pad_up_bitrate_bps(pad_up_bitrate_bps),
mflodman48a4beb2016-07-01 13:03:59 +0200140 allocated_bitrate_bps(-1),
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800141 media_ratio(1.0),
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100142 bitrate_priority(bitrate_priority) {}
mflodman48a4beb2016-07-01 13:03:59 +0200143
144 BitrateAllocatorObserver* observer;
perkj71ee44c2016-06-15 00:47:53 -0700145 uint32_t pad_up_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200146 int64_t allocated_bitrate_bps;
147 double media_ratio; // Part of the total bitrate used for media [0.0, 1.0].
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800148 // The amount of bitrate allocated to this observer relative to all other
149 // observers. If an observer has twice the bitrate_priority of other
150 // observers, it should be allocated twice the bitrate above its min.
151 double bitrate_priority;
srte1eb051c2017-11-29 11:23:59 +0100152
153 uint32_t LastAllocatedBitrate() const;
154 // The minimum bitrate required by this observer, including
155 // enable-hysteresis if the observer is in a paused state.
156 uint32_t MinBitrateWithHysteresis() const;
mflodman2ebe5b12016-05-13 01:43:51 -0700157 };
158
perkj71ee44c2016-06-15 00:47:53 -0700159 // Calculates the minimum requested send bitrate and max padding bitrate and
160 // calls LimitObserver::OnAllocationLimitsChanged.
Niels Möllerd4043f62018-04-26 16:06:22 +0200161 void UpdateAllocationLimits() RTC_RUN_ON(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700162
mflodman48a4beb2016-07-01 13:03:59 +0200163 typedef std::vector<ObserverConfig> ObserverConfigs;
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200164 ObserverConfigs::const_iterator FindObserverConfig(
165 const BitrateAllocatorObserver* observer) const
166 RTC_RUN_ON(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200167 ObserverConfigs::iterator FindObserverConfig(
Niels Möllerd4043f62018-04-26 16:06:22 +0200168 const BitrateAllocatorObserver* observer) RTC_RUN_ON(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700169
170 typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap;
171 typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
172
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200173 ObserverAllocation AllocateBitrates(uint32_t bitrate) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200174 RTC_RUN_ON(&sequenced_checker_);
Stefan Holmere5904162015-03-26 11:11:06 +0100175
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800176 // Allocates zero bitrate to all observers.
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200177 ObserverAllocation ZeroRateAllocation() const RTC_RUN_ON(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800178 // Allocates bitrate to observers when there isn't enough to allocate the
179 // minimum to all observers.
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200180 ObserverAllocation LowRateAllocation(uint32_t bitrate) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200181 RTC_RUN_ON(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800182 // Allocates bitrate to all observers when the available bandwidth is enough
183 // to allocate the minimum to all observers but not enough to allocate the
184 // max bitrate of each observer.
mflodman101f2502016-06-09 17:21:19 +0200185 ObserverAllocation NormalRateAllocation(uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200186 uint32_t sum_min_bitrates) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200187 RTC_RUN_ON(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800188 // Allocates bitrate to observers when there is enough available bandwidth
189 // for all observers to be allocated their max bitrate.
mflodman101f2502016-06-09 17:21:19 +0200190 ObserverAllocation MaxRateAllocation(uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200191 uint32_t sum_max_bitrates) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200192 RTC_RUN_ON(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200193
mflodman101f2502016-06-09 17:21:19 +0200194 // Splits |bitrate| evenly to observers already in |allocation|.
195 // |include_zero_allocations| decides if zero allocations should be part of
196 // the distribution or not. The allowed max bitrate is |max_multiplier| x
197 // observer max bitrate.
198 void DistributeBitrateEvenly(uint32_t bitrate,
199 bool include_zero_allocations,
200 int max_multiplier,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200201 ObserverAllocation* allocation) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200202 RTC_RUN_ON(&sequenced_checker_);
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200203 bool EnoughBitrateForAllObservers(uint32_t bitrate,
204 uint32_t sum_min_bitrates) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200205 RTC_RUN_ON(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200206
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800207 // From the available |bitrate|, each observer will be allocated a
208 // proportional amount based upon its bitrate priority. If that amount is
209 // more than the observer's capacity, it will be allocated its capacity, and
210 // the excess bitrate is still allocated proportionally to other observers.
211 // Allocating the proportional amount means an observer with twice the
212 // bitrate_priority of another will be allocated twice the bitrate.
213 void DistributeBitrateRelatively(
214 uint32_t bitrate,
215 const ObserverAllocation& observers_capacities,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200216 ObserverAllocation* allocation) const RTC_RUN_ON(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800217
Ying Wanga646d302018-03-02 17:04:11 +0100218 // Allow packets to be transmitted in up to 2 times max video bitrate if the
219 // bandwidth estimate allows it.
220 // TODO(bugs.webrtc.org/8541): May be worth to refactor to keep this logic in
221 // video send stream. Similar logic is implemented in
222 // AudioPriorityBitrateAllocationStrategy.
Niels Möller74e5f802018-04-25 14:03:46 +0200223 static uint8_t GetTransmissionMaxBitrateMultiplier();
Ying Wanga646d302018-03-02 17:04:11 +0100224
perkj26091b12016-09-01 01:17:40 -0700225 rtc::SequencedTaskChecker sequenced_checker_;
danilchapa37de392017-09-09 04:17:22 -0700226 LimitObserver* const limit_observer_ RTC_GUARDED_BY(&sequenced_checker_);
Stefan Holmere5904162015-03-26 11:11:06 +0100227 // Stored in a list to keep track of the insertion order.
danilchapa37de392017-09-09 04:17:22 -0700228 ObserverConfigs bitrate_observer_configs_ RTC_GUARDED_BY(&sequenced_checker_);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100229 uint32_t last_target_bps_ RTC_GUARDED_BY(&sequenced_checker_);
230 uint32_t last_link_capacity_bps_ RTC_GUARDED_BY(&sequenced_checker_);
danilchapa37de392017-09-09 04:17:22 -0700231 uint32_t last_non_zero_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
232 uint8_t last_fraction_loss_ RTC_GUARDED_BY(&sequenced_checker_);
233 int64_t last_rtt_ RTC_GUARDED_BY(&sequenced_checker_);
234 int64_t last_bwe_period_ms_ RTC_GUARDED_BY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200235 // Number of mute events based on too low BWE, not network up/down.
danilchapa37de392017-09-09 04:17:22 -0700236 int num_pause_events_ RTC_GUARDED_BY(&sequenced_checker_);
237 Clock* const clock_ RTC_GUARDED_BY(&sequenced_checker_);
238 int64_t last_bwe_log_time_ RTC_GUARDED_BY(&sequenced_checker_);
239 uint32_t total_requested_padding_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
240 uint32_t total_requested_min_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200241 uint32_t total_requested_max_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200242 std::unique_ptr<rtc::BitrateAllocationStrategy> bitrate_allocation_strategy_
243 RTC_GUARDED_BY(&sequenced_checker_);
Niels Möller74e5f802018-04-25 14:03:46 +0200244 const uint8_t transmission_max_bitrate_multiplier_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000245};
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800246
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000247} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200248#endif // CALL_BITRATE_ALLOCATOR_H_