blob: dbdd14f84b068dc02880598cb60088bcacd5d3b4 [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 Jansson13e59032018-11-21 19:13:07 +010022#include "api/units/data_rate.h"
23#include "api/units/time_delta.h"
Alex Narest78609d52017-10-20 10:37:47 +020024#include "rtc_base/bitrateallocationstrategy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/sequenced_task_checker.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000026
27namespace webrtc {
28
mflodman48a4beb2016-07-01 13:03:59 +020029class Clock;
30
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020031struct BitrateAllocationUpdate {
Sebastian Jansson13e59032018-11-21 19:13:07 +010032 DataRate target_bitrate = DataRate::Zero();
33 DataRate link_capacity = DataRate::Zero();
34 double packet_loss_ratio = 0;
35 TimeDelta round_trip_time = TimeDelta::PlusInfinity();
36 TimeDelta bwe_period = TimeDelta::PlusInfinity();
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020037};
mflodman86aabb22016-03-11 15:44:32 +010038// Used by all send streams with adaptive bitrate, to get the currently
39// allocated bitrate for the send stream. The current network properties are
40// given at the same time, to let the send stream decide about possible loss
41// protection.
42class BitrateAllocatorObserver {
43 public:
mflodman48a4beb2016-07-01 13:03:59 +020044 // Returns the amount of protection used by the BitrateAllocatorObserver
45 // implementation, as bitrate in bps.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020046 virtual uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) = 0;
minyue78b4d562016-11-30 04:47:39 -080047
perkj71ee44c2016-06-15 00:47:53 -070048 protected:
mflodman86aabb22016-03-11 15:44:32 +010049 virtual ~BitrateAllocatorObserver() {}
50};
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000051
Sebastian Jansson24ad7202018-04-19 08:25:12 +020052// Struct describing parameters for how a media stream should get bitrate
53// allocated to it. |min_bitrate_bps| = 0 equals no min bitrate.
54// |max_bitrate_bps| = 0 equals no max bitrate.
55// |enforce_min_bitrate| = 'true' will allocate at least |min_bitrate_bps| for
56// this observer, even if the BWE is too low, 'false' will allocate 0 to
57// the observer if BWE doesn't allow |min_bitrate_bps|.
58// |has_packet_feedback| indicates whether the data produced by the
59// corresponding media stream will receive per packet feedback. This is
60// tracked here to communicate to limit observers whether packet feedback can
61// be expected, which is true if any of the active observers has packet
62// feedback enabled. Note that |observer|->OnBitrateUpdated() will be called
63// within the scope of this method with the current rtt, fraction_loss and
64// available bitrate and that the bitrate in OnBitrateUpdated will be zero if
65// the |observer| is currently not allowed to send data.
66struct MediaStreamAllocationConfig {
67 uint32_t min_bitrate_bps;
68 uint32_t max_bitrate_bps;
69 uint32_t pad_up_bitrate_bps;
70 bool enforce_min_bitrate;
71 std::string track_id;
72 double bitrate_priority;
73 bool has_packet_feedback;
74};
75
Sebastian Jansson83267802018-04-19 08:27:19 +020076// Interface used for mocking
77class BitrateAllocatorInterface {
78 public:
79 virtual void AddObserver(BitrateAllocatorObserver* observer,
80 MediaStreamAllocationConfig config) = 0;
81 virtual void RemoveObserver(BitrateAllocatorObserver* observer) = 0;
Sebastian Jansson44a262a2018-10-24 16:07:20 +020082 virtual int GetStartBitrate(BitrateAllocatorObserver* observer) const = 0;
Sebastian Jansson83267802018-04-19 08:27:19 +020083
84 protected:
85 virtual ~BitrateAllocatorInterface() = default;
86};
87
mflodman86aabb22016-03-11 15:44:32 +010088// Usage: this class will register multiple RtcpBitrateObserver's one at each
89// RTCP module. It will aggregate the results and run one bandwidth estimation
90// and push the result to the encoders via BitrateAllocatorObserver(s).
Sebastian Jansson83267802018-04-19 08:27:19 +020091class BitrateAllocator : public BitrateAllocatorInterface {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000092 public:
perkj71ee44c2016-06-15 00:47:53 -070093 // Used to get notified when send stream limits such as the minimum send
94 // bitrate and max padding bitrate is changed.
95 class LimitObserver {
96 public:
Sebastian Jansson35fa2802018-10-01 09:16:12 +020097 virtual void OnAllocationLimitsChanged(
98 uint32_t min_send_bitrate_bps,
99 uint32_t max_padding_bitrate_bps,
100 uint32_t total_bitrate_bps,
101 uint32_t allocated_without_feedback_bps,
102 bool has_packet_feedback) = 0;
perkj71ee44c2016-06-15 00:47:53 -0700103
104 protected:
Sebastian Jansson83267802018-04-19 08:27:19 +0200105 virtual ~LimitObserver() = default;
perkj71ee44c2016-06-15 00:47:53 -0700106 };
107
108 explicit BitrateAllocator(LimitObserver* limit_observer);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200109 ~BitrateAllocator() override;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000110
mflodman86aabb22016-03-11 15:44:32 +0100111 // Allocate target_bitrate across the registered BitrateAllocatorObservers.
perkj71ee44c2016-06-15 00:47:53 -0700112 void OnNetworkChanged(uint32_t target_bitrate_bps,
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100113 uint32_t link_capacity_bps,
perkj71ee44c2016-06-15 00:47:53 -0700114 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800115 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -0700116 int64_t bwe_period_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000117
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100118 // Set the configuration used by the bandwidth management.
Peter Boström8e4e8b02015-09-15 15:08:03 +0200119 // |observer| updates bitrates if already in use.
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200120 // |config| is the configuration to use for allocation.
perkj57c21f92016-06-17 07:27:16 -0700121 void AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson83267802018-04-19 08:27:19 +0200122 MediaStreamAllocationConfig config) override;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000123
mflodman101f2502016-06-09 17:21:19 +0200124 // Removes a previously added observer, but will not trigger a new bitrate
125 // allocation.
Sebastian Jansson83267802018-04-19 08:27:19 +0200126 void RemoveObserver(BitrateAllocatorObserver* observer) override;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000127
perkj57c21f92016-06-17 07:27:16 -0700128 // Returns initial bitrate allocated for |observer|. If |observer| is not in
129 // the list of added observers, a best guess is returned.
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200130 int GetStartBitrate(BitrateAllocatorObserver* observer) const override;
perkj57c21f92016-06-17 07:27:16 -0700131
Alex Narest78609d52017-10-20 10:37:47 +0200132 // Sets external allocation strategy. If strategy is not set default WebRTC
133 // allocation mechanism will be used. The strategy may be changed during call.
134 // Setting NULL value will restore default WEBRTC allocation strategy.
135 void SetBitrateAllocationStrategy(
136 std::unique_ptr<rtc::BitrateAllocationStrategy>
137 bitrate_allocation_strategy);
138
mflodman2ebe5b12016-05-13 01:43:51 -0700139 private:
Alex Narest78609d52017-10-20 10:37:47 +0200140 struct ObserverConfig : rtc::BitrateAllocationStrategy::TrackConfig {
mflodman2ebe5b12016-05-13 01:43:51 -0700141 ObserverConfig(BitrateAllocatorObserver* observer,
142 uint32_t min_bitrate_bps,
143 uint32_t max_bitrate_bps,
perkj71ee44c2016-06-15 00:47:53 -0700144 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200145 bool enforce_min_bitrate,
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800146 std::string track_id,
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100147 double bitrate_priority,
148 bool has_packet_feedback)
Alex Narest78609d52017-10-20 10:37:47 +0200149 : TrackConfig(min_bitrate_bps,
150 max_bitrate_bps,
151 enforce_min_bitrate,
152 track_id),
153 observer(observer),
perkj71ee44c2016-06-15 00:47:53 -0700154 pad_up_bitrate_bps(pad_up_bitrate_bps),
mflodman48a4beb2016-07-01 13:03:59 +0200155 allocated_bitrate_bps(-1),
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800156 media_ratio(1.0),
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100157 bitrate_priority(bitrate_priority),
158 has_packet_feedback(has_packet_feedback) {}
mflodman48a4beb2016-07-01 13:03:59 +0200159
160 BitrateAllocatorObserver* observer;
perkj71ee44c2016-06-15 00:47:53 -0700161 uint32_t pad_up_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200162 int64_t allocated_bitrate_bps;
163 double media_ratio; // Part of the total bitrate used for media [0.0, 1.0].
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800164 // The amount of bitrate allocated to this observer relative to all other
165 // observers. If an observer has twice the bitrate_priority of other
166 // observers, it should be allocated twice the bitrate above its min.
167 double bitrate_priority;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100168 bool has_packet_feedback;
srte1eb051c2017-11-29 11:23:59 +0100169
170 uint32_t LastAllocatedBitrate() const;
171 // The minimum bitrate required by this observer, including
172 // enable-hysteresis if the observer is in a paused state.
173 uint32_t MinBitrateWithHysteresis() const;
mflodman2ebe5b12016-05-13 01:43:51 -0700174 };
175
perkj71ee44c2016-06-15 00:47:53 -0700176 // Calculates the minimum requested send bitrate and max padding bitrate and
177 // calls LimitObserver::OnAllocationLimitsChanged.
Niels Möllerd4043f62018-04-26 16:06:22 +0200178 void UpdateAllocationLimits() RTC_RUN_ON(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700179
mflodman48a4beb2016-07-01 13:03:59 +0200180 typedef std::vector<ObserverConfig> ObserverConfigs;
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200181 ObserverConfigs::const_iterator FindObserverConfig(
182 const BitrateAllocatorObserver* observer) const
183 RTC_RUN_ON(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200184 ObserverConfigs::iterator FindObserverConfig(
Niels Möllerd4043f62018-04-26 16:06:22 +0200185 const BitrateAllocatorObserver* observer) RTC_RUN_ON(&sequenced_checker_);
mflodman2ebe5b12016-05-13 01:43:51 -0700186
187 typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap;
188 typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
189
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200190 ObserverAllocation AllocateBitrates(uint32_t bitrate) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200191 RTC_RUN_ON(&sequenced_checker_);
Stefan Holmere5904162015-03-26 11:11:06 +0100192
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800193 // Allocates zero bitrate to all observers.
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200194 ObserverAllocation ZeroRateAllocation() const RTC_RUN_ON(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800195 // Allocates bitrate to observers when there isn't enough to allocate the
196 // minimum to all observers.
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200197 ObserverAllocation LowRateAllocation(uint32_t bitrate) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200198 RTC_RUN_ON(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800199 // Allocates bitrate to all observers when the available bandwidth is enough
200 // to allocate the minimum to all observers but not enough to allocate the
201 // max bitrate of each observer.
mflodman101f2502016-06-09 17:21:19 +0200202 ObserverAllocation NormalRateAllocation(uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200203 uint32_t sum_min_bitrates) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200204 RTC_RUN_ON(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800205 // Allocates bitrate to observers when there is enough available bandwidth
206 // for all observers to be allocated their max bitrate.
mflodman101f2502016-06-09 17:21:19 +0200207 ObserverAllocation MaxRateAllocation(uint32_t bitrate,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200208 uint32_t sum_max_bitrates) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200209 RTC_RUN_ON(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200210
mflodman101f2502016-06-09 17:21:19 +0200211 // Splits |bitrate| evenly to observers already in |allocation|.
212 // |include_zero_allocations| decides if zero allocations should be part of
213 // the distribution or not. The allowed max bitrate is |max_multiplier| x
214 // observer max bitrate.
215 void DistributeBitrateEvenly(uint32_t bitrate,
216 bool include_zero_allocations,
217 int max_multiplier,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200218 ObserverAllocation* allocation) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200219 RTC_RUN_ON(&sequenced_checker_);
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200220 bool EnoughBitrateForAllObservers(uint32_t bitrate,
221 uint32_t sum_min_bitrates) const
Niels Möllerd4043f62018-04-26 16:06:22 +0200222 RTC_RUN_ON(&sequenced_checker_);
mflodman101f2502016-06-09 17:21:19 +0200223
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800224 // From the available |bitrate|, each observer will be allocated a
225 // proportional amount based upon its bitrate priority. If that amount is
226 // more than the observer's capacity, it will be allocated its capacity, and
227 // the excess bitrate is still allocated proportionally to other observers.
228 // Allocating the proportional amount means an observer with twice the
229 // bitrate_priority of another will be allocated twice the bitrate.
230 void DistributeBitrateRelatively(
231 uint32_t bitrate,
232 const ObserverAllocation& observers_capacities,
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200233 ObserverAllocation* allocation) const RTC_RUN_ON(&sequenced_checker_);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800234
Ying Wanga646d302018-03-02 17:04:11 +0100235 // Allow packets to be transmitted in up to 2 times max video bitrate if the
236 // bandwidth estimate allows it.
237 // TODO(bugs.webrtc.org/8541): May be worth to refactor to keep this logic in
238 // video send stream. Similar logic is implemented in
239 // AudioPriorityBitrateAllocationStrategy.
Niels Möller74e5f802018-04-25 14:03:46 +0200240 static uint8_t GetTransmissionMaxBitrateMultiplier();
Ying Wanga646d302018-03-02 17:04:11 +0100241
perkj26091b12016-09-01 01:17:40 -0700242 rtc::SequencedTaskChecker sequenced_checker_;
danilchapa37de392017-09-09 04:17:22 -0700243 LimitObserver* const limit_observer_ RTC_GUARDED_BY(&sequenced_checker_);
Stefan Holmere5904162015-03-26 11:11:06 +0100244 // Stored in a list to keep track of the insertion order.
danilchapa37de392017-09-09 04:17:22 -0700245 ObserverConfigs bitrate_observer_configs_ RTC_GUARDED_BY(&sequenced_checker_);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100246 uint32_t last_target_bps_ RTC_GUARDED_BY(&sequenced_checker_);
247 uint32_t last_link_capacity_bps_ RTC_GUARDED_BY(&sequenced_checker_);
danilchapa37de392017-09-09 04:17:22 -0700248 uint32_t last_non_zero_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
249 uint8_t last_fraction_loss_ RTC_GUARDED_BY(&sequenced_checker_);
250 int64_t last_rtt_ RTC_GUARDED_BY(&sequenced_checker_);
251 int64_t last_bwe_period_ms_ RTC_GUARDED_BY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200252 // Number of mute events based on too low BWE, not network up/down.
danilchapa37de392017-09-09 04:17:22 -0700253 int num_pause_events_ RTC_GUARDED_BY(&sequenced_checker_);
254 Clock* const clock_ RTC_GUARDED_BY(&sequenced_checker_);
255 int64_t last_bwe_log_time_ RTC_GUARDED_BY(&sequenced_checker_);
256 uint32_t total_requested_padding_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
257 uint32_t total_requested_min_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200258 uint32_t total_requested_max_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
Sebastian Jansson35fa2802018-10-01 09:16:12 +0200259 uint32_t allocated_without_feedback_ RTC_GUARDED_BY(&sequenced_checker_);
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100260 bool has_packet_feedback_ RTC_GUARDED_BY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200261 std::unique_ptr<rtc::BitrateAllocationStrategy> bitrate_allocation_strategy_
262 RTC_GUARDED_BY(&sequenced_checker_);
Niels Möller74e5f802018-04-25 14:03:46 +0200263 const uint8_t transmission_max_bitrate_multiplier_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000264};
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800265
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000266} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200267#endif // CALL_BITRATE_ALLOCATOR_H_