blob: 769ab0faef67e052dba206fe01848ebb8974cdf9 [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"
Sebastian Janssonb55015e2019-04-09 13:44:04 +020023#include "rtc_base/synchronization/sequence_checker.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000024
25namespace webrtc {
26
mflodman48a4beb2016-07-01 13:03:59 +020027class Clock;
28
mflodman86aabb22016-03-11 15:44:32 +010029// Used by all send streams with adaptive bitrate, to get the currently
30// allocated bitrate for the send stream. The current network properties are
31// given at the same time, to let the send stream decide about possible loss
32// protection.
33class BitrateAllocatorObserver {
34 public:
mflodman48a4beb2016-07-01 13:03:59 +020035 // Returns the amount of protection used by the BitrateAllocatorObserver
36 // implementation, as bitrate in bps.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020037 virtual uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) = 0;
minyue78b4d562016-11-30 04:47:39 -080038
perkj71ee44c2016-06-15 00:47:53 -070039 protected:
mflodman86aabb22016-03-11 15:44:32 +010040 virtual ~BitrateAllocatorObserver() {}
41};
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000042
Sebastian Jansson24ad7202018-04-19 08:25:12 +020043// Struct describing parameters for how a media stream should get bitrate
Sebastian Jansson4d461ba2019-09-17 20:53:26 +020044// allocated to it.
45
Sebastian Jansson24ad7202018-04-19 08:25:12 +020046struct MediaStreamAllocationConfig {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +020047 // Minimum bitrate supported by track. 0 equals no min bitrate.
Sebastian Jansson24ad7202018-04-19 08:25:12 +020048 uint32_t min_bitrate_bps;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +020049 // Maximum bitrate supported by track. 0 equals no max bitrate.
Sebastian Jansson24ad7202018-04-19 08:25:12 +020050 uint32_t max_bitrate_bps;
51 uint32_t pad_up_bitrate_bps;
Sebastian Jansson464a5572019-02-12 13:32:32 +010052 int64_t priority_bitrate_bps;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +020053 // True means track may not be paused by allocating 0 bitrate will allocate at
54 // least |min_bitrate_bps| for this observer, even if the BWE is too low,
55 // false will allocate 0 to the observer if BWE doesn't allow
56 // |min_bitrate_bps|.
Sebastian Jansson24ad7202018-04-19 08:25:12 +020057 bool enforce_min_bitrate;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +020058 // The amount of bitrate allocated to this observer relative to all other
59 // observers. If an observer has twice the bitrate_priority of other
60 // observers, it should be allocated twice the bitrate above its min.
Sebastian Jansson24ad7202018-04-19 08:25:12 +020061 double bitrate_priority;
Sebastian Jansson24ad7202018-04-19 08:25:12 +020062};
63
Sebastian Jansson83267802018-04-19 08:27:19 +020064// Interface used for mocking
65class BitrateAllocatorInterface {
66 public:
67 virtual void AddObserver(BitrateAllocatorObserver* observer,
68 MediaStreamAllocationConfig config) = 0;
69 virtual void RemoveObserver(BitrateAllocatorObserver* observer) = 0;
Sebastian Jansson44a262a2018-10-24 16:07:20 +020070 virtual int GetStartBitrate(BitrateAllocatorObserver* observer) const = 0;
Sebastian Jansson83267802018-04-19 08:27:19 +020071
72 protected:
73 virtual ~BitrateAllocatorInterface() = default;
74};
75
mflodman86aabb22016-03-11 15:44:32 +010076// Usage: this class will register multiple RtcpBitrateObserver's one at each
77// RTCP module. It will aggregate the results and run one bandwidth estimation
78// and push the result to the encoders via BitrateAllocatorObserver(s).
Sebastian Jansson83267802018-04-19 08:27:19 +020079class BitrateAllocator : public BitrateAllocatorInterface {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000080 public:
perkj71ee44c2016-06-15 00:47:53 -070081 // Used to get notified when send stream limits such as the minimum send
82 // bitrate and max padding bitrate is changed.
83 class LimitObserver {
84 public:
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +010085 virtual void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
86 uint32_t max_padding_bitrate_bps,
87 uint32_t total_bitrate_bps) = 0;
perkj71ee44c2016-06-15 00:47:53 -070088
89 protected:
Sebastian Jansson83267802018-04-19 08:27:19 +020090 virtual ~LimitObserver() = default;
perkj71ee44c2016-06-15 00:47:53 -070091 };
92
Sebastian Janssonda6806c2019-03-04 17:05:12 +010093 BitrateAllocator(Clock* clock, LimitObserver* limit_observer);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020094 ~BitrateAllocator() override;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000095
Sebastian Jansson2701bc92018-12-11 15:02:47 +010096 void UpdateStartRate(uint32_t start_rate_bps);
97
mflodman86aabb22016-03-11 15:44:32 +010098 // Allocate target_bitrate across the registered BitrateAllocatorObservers.
perkj71ee44c2016-06-15 00:47:53 -070099 void OnNetworkChanged(uint32_t target_bitrate_bps,
Florent Castelli4e615d52019-08-22 16:09:06 +0200100 uint32_t stable_target_bitrate_bps,
101 uint32_t bandwidth_bps,
perkj71ee44c2016-06-15 00:47:53 -0700102 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800103 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -0700104 int64_t bwe_period_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000105
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100106 // Set the configuration used by the bandwidth management.
Peter Boström8e4e8b02015-09-15 15:08:03 +0200107 // |observer| updates bitrates if already in use.
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200108 // |config| is the configuration to use for allocation.
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200109 // Note that |observer|->OnBitrateUpdated() will be called
110 // within the scope of this method with the current rtt, fraction_loss and
111 // available bitrate and that the bitrate in OnBitrateUpdated will be zero if
112 // the |observer| is currently not allowed to send data.
perkj57c21f92016-06-17 07:27:16 -0700113 void AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson83267802018-04-19 08:27:19 +0200114 MediaStreamAllocationConfig config) override;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000115
mflodman101f2502016-06-09 17:21:19 +0200116 // Removes a previously added observer, but will not trigger a new bitrate
117 // allocation.
Sebastian Jansson83267802018-04-19 08:27:19 +0200118 void RemoveObserver(BitrateAllocatorObserver* observer) override;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000119
perkj57c21f92016-06-17 07:27:16 -0700120 // Returns initial bitrate allocated for |observer|. If |observer| is not in
121 // the list of added observers, a best guess is returned.
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200122 int GetStartBitrate(BitrateAllocatorObserver* observer) const override;
perkj57c21f92016-06-17 07:27:16 -0700123
mflodman2ebe5b12016-05-13 01:43:51 -0700124 private:
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200125 struct AllocatableTrack {
126 AllocatableTrack(BitrateAllocatorObserver* observer,
127 MediaStreamAllocationConfig allocation_config)
Jonas Olsson0182a032019-07-09 12:31:20 +0200128 : observer(observer),
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200129 config(allocation_config),
mflodman48a4beb2016-07-01 13:03:59 +0200130 allocated_bitrate_bps(-1),
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200131 media_ratio(1.0) {}
mflodman48a4beb2016-07-01 13:03:59 +0200132 BitrateAllocatorObserver* observer;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200133 MediaStreamAllocationConfig config;
mflodman48a4beb2016-07-01 13:03:59 +0200134 int64_t allocated_bitrate_bps;
135 double media_ratio; // Part of the total bitrate used for media [0.0, 1.0].
Jonas Olsson0182a032019-07-09 12:31:20 +0200136
srte1eb051c2017-11-29 11:23:59 +0100137 uint32_t LastAllocatedBitrate() const;
138 // The minimum bitrate required by this observer, including
139 // enable-hysteresis if the observer is in a paused state.
140 uint32_t MinBitrateWithHysteresis() const;
mflodman2ebe5b12016-05-13 01:43:51 -0700141 };
142
perkj71ee44c2016-06-15 00:47:53 -0700143 // Calculates the minimum requested send bitrate and max padding bitrate and
144 // calls LimitObserver::OnAllocationLimitsChanged.
Niels Möllerd4043f62018-04-26 16:06:22 +0200145 void UpdateAllocationLimits() RTC_RUN_ON(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700146
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200147 typedef std::multimap<uint32_t, const AllocatableTrack*> ObserverSortingMap;
mflodman2ebe5b12016-05-13 01:43:51 -0700148 typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
149
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200150 ObserverAllocation AllocateBitrates(uint32_t bitrate) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200151 RTC_RUN_ON(&sequenced_checker_);
Stefan Holmere5904162015-03-26 11:11:06 +0100152
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800153 // Allocates zero bitrate to all observers.
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200154 ObserverAllocation ZeroRateAllocation() const RTC_RUN_ON(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800155 // Allocates bitrate to observers when there isn't enough to allocate the
156 // minimum to all observers.
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200157 ObserverAllocation LowRateAllocation(uint32_t bitrate) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200158 RTC_RUN_ON(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800159 // Allocates bitrate to all observers when the available bandwidth is enough
160 // to allocate the minimum to all observers but not enough to allocate the
161 // max bitrate of each observer.
mflodman101f2502016-06-09 17:21:19 +0200162 ObserverAllocation NormalRateAllocation(uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200163 uint32_t sum_min_bitrates) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200164 RTC_RUN_ON(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800165 // Allocates bitrate to observers when there is enough available bandwidth
166 // for all observers to be allocated their max bitrate.
mflodman101f2502016-06-09 17:21:19 +0200167 ObserverAllocation MaxRateAllocation(uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200168 uint32_t sum_max_bitrates) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200169 RTC_RUN_ON(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200170
mflodman101f2502016-06-09 17:21:19 +0200171 // Splits |bitrate| evenly to observers already in |allocation|.
172 // |include_zero_allocations| decides if zero allocations should be part of
173 // the distribution or not. The allowed max bitrate is |max_multiplier| x
174 // observer max bitrate.
175 void DistributeBitrateEvenly(uint32_t bitrate,
176 bool include_zero_allocations,
177 int max_multiplier,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200178 ObserverAllocation* allocation) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200179 RTC_RUN_ON(&sequenced_checker_);
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200180 bool EnoughBitrateForAllObservers(uint32_t bitrate,
181 uint32_t sum_min_bitrates) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200182 RTC_RUN_ON(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200183
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800184 // From the available |bitrate|, each observer will be allocated a
185 // proportional amount based upon its bitrate priority. If that amount is
186 // more than the observer's capacity, it will be allocated its capacity, and
187 // the excess bitrate is still allocated proportionally to other observers.
188 // Allocating the proportional amount means an observer with twice the
189 // bitrate_priority of another will be allocated twice the bitrate.
190 void DistributeBitrateRelatively(
191 uint32_t bitrate,
192 const ObserverAllocation& observers_capacities,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200193 ObserverAllocation* allocation) const RTC_RUN_ON(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800194
Ying Wanga646d302018-03-02 17:04:11 +0100195 // Allow packets to be transmitted in up to 2 times max video bitrate if the
196 // bandwidth estimate allows it.
197 // TODO(bugs.webrtc.org/8541): May be worth to refactor to keep this logic in
Jonas Olsson0182a032019-07-09 12:31:20 +0200198 // video send stream.
Niels Möller74e5f802018-04-25 14:03:46 +0200199 static uint8_t GetTransmissionMaxBitrateMultiplier();
Ying Wanga646d302018-03-02 17:04:11 +0100200
Sebastian Janssonb55015e2019-04-09 13:44:04 +0200201 SequenceChecker sequenced_checker_;
danilchapa37de392017-09-09 04:17:22 -0700202 LimitObserver* const limit_observer_ RTC_GUARDED_BY(&sequenced_checker_);
Stefan Holmere5904162015-03-26 11:11:06 +0100203 // Stored in a list to keep track of the insertion order.
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200204 std::vector<AllocatableTrack> allocatable_tracks_
205 RTC_GUARDED_BY(&sequenced_checker_);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100206 uint32_t last_target_bps_ RTC_GUARDED_BY(&sequenced_checker_);
Florent Castelli4e615d52019-08-22 16:09:06 +0200207 uint32_t last_stable_target_bps_ RTC_GUARDED_BY(&sequenced_checker_);
208 uint32_t last_bandwidth_bps_ RTC_GUARDED_BY(&sequenced_checker_);
danilchapa37de392017-09-09 04:17:22 -0700209 uint32_t last_non_zero_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
210 uint8_t last_fraction_loss_ RTC_GUARDED_BY(&sequenced_checker_);
211 int64_t last_rtt_ RTC_GUARDED_BY(&sequenced_checker_);
212 int64_t last_bwe_period_ms_ RTC_GUARDED_BY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200213 // Number of mute events based on too low BWE, not network up/down.
danilchapa37de392017-09-09 04:17:22 -0700214 int num_pause_events_ RTC_GUARDED_BY(&sequenced_checker_);
215 Clock* const clock_ RTC_GUARDED_BY(&sequenced_checker_);
216 int64_t last_bwe_log_time_ RTC_GUARDED_BY(&sequenced_checker_);
217 uint32_t total_requested_padding_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
218 uint32_t total_requested_min_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200219 uint32_t total_requested_max_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
Niels Möller74e5f802018-04-25 14:03:46 +0200220 const uint8_t transmission_max_bitrate_multiplier_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000221};
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800222
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000223} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200224#endif // CALL_BITRATE_ALLOCATOR_H_