blob: ac98210e2f4764671d3a2a77e864ab0a77095cf4 [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
Alex Narest78609d52017-10-20 10:37:47 +020022#include "rtc_base/bitrateallocationstrategy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/sequenced_task_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.
37 virtual uint32_t OnBitrateUpdated(uint32_t bitrate_bps,
38 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080039 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070040 int64_t bwe_period_ms) = 0;
minyue78b4d562016-11-30 04:47:39 -080041
perkj71ee44c2016-06-15 00:47:53 -070042 protected:
mflodman86aabb22016-03-11 15:44:32 +010043 virtual ~BitrateAllocatorObserver() {}
44};
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000045
Sebastian Jansson24ad7202018-04-19 08:25:12 +020046// Struct describing parameters for how a media stream should get bitrate
47// allocated to it. |min_bitrate_bps| = 0 equals no min bitrate.
48// |max_bitrate_bps| = 0 equals no max bitrate.
49// |enforce_min_bitrate| = 'true' will allocate at least |min_bitrate_bps| for
50// this observer, even if the BWE is too low, 'false' will allocate 0 to
51// the observer if BWE doesn't allow |min_bitrate_bps|.
52// |has_packet_feedback| indicates whether the data produced by the
53// corresponding media stream will receive per packet feedback. This is
54// tracked here to communicate to limit observers whether packet feedback can
55// be expected, which is true if any of the active observers has packet
56// feedback enabled. Note that |observer|->OnBitrateUpdated() will be called
57// within the scope of this method with the current rtt, fraction_loss and
58// available bitrate and that the bitrate in OnBitrateUpdated will be zero if
59// the |observer| is currently not allowed to send data.
60struct MediaStreamAllocationConfig {
61 uint32_t min_bitrate_bps;
62 uint32_t max_bitrate_bps;
63 uint32_t pad_up_bitrate_bps;
64 bool enforce_min_bitrate;
65 std::string track_id;
66 double bitrate_priority;
67 bool has_packet_feedback;
68};
69
Sebastian Jansson83267802018-04-19 08:27:19 +020070// Interface used for mocking
71class BitrateAllocatorInterface {
72 public:
73 virtual void AddObserver(BitrateAllocatorObserver* observer,
74 MediaStreamAllocationConfig config) = 0;
75 virtual void RemoveObserver(BitrateAllocatorObserver* observer) = 0;
76 virtual int GetStartBitrate(BitrateAllocatorObserver* observer) = 0;
77
78 protected:
79 virtual ~BitrateAllocatorInterface() = default;
80};
81
mflodman86aabb22016-03-11 15:44:32 +010082// Usage: this class will register multiple RtcpBitrateObserver's one at each
83// RTCP module. It will aggregate the results and run one bandwidth estimation
84// and push the result to the encoders via BitrateAllocatorObserver(s).
Sebastian Jansson83267802018-04-19 08:27:19 +020085class BitrateAllocator : public BitrateAllocatorInterface {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000086 public:
perkj71ee44c2016-06-15 00:47:53 -070087 // Used to get notified when send stream limits such as the minimum send
88 // bitrate and max padding bitrate is changed.
89 class LimitObserver {
90 public:
philipelf69e7682018-02-28 13:06:28 +010091 virtual void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
92 uint32_t max_padding_bitrate_bps,
Sebastian Janssonfe617a32018-03-21 12:45:20 +010093 uint32_t total_bitrate_bps,
94 bool has_packet_feedback) = 0;
perkj71ee44c2016-06-15 00:47:53 -070095
96 protected:
Sebastian Jansson83267802018-04-19 08:27:19 +020097 virtual ~LimitObserver() = default;
perkj71ee44c2016-06-15 00:47:53 -070098 };
99
100 explicit BitrateAllocator(LimitObserver* limit_observer);
mflodman48a4beb2016-07-01 13:03:59 +0200101 ~BitrateAllocator();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000102
mflodman86aabb22016-03-11 15:44:32 +0100103 // Allocate target_bitrate across the registered BitrateAllocatorObservers.
perkj71ee44c2016-06-15 00:47:53 -0700104 void OnNetworkChanged(uint32_t target_bitrate_bps,
105 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800106 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -0700107 int64_t bwe_period_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000108
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100109 // Set the configuration used by the bandwidth management.
Peter Boström8e4e8b02015-09-15 15:08:03 +0200110 // |observer| updates bitrates if already in use.
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200111 // |config| is the configuration to use for allocation.
perkj57c21f92016-06-17 07:27:16 -0700112 void AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson83267802018-04-19 08:27:19 +0200113 MediaStreamAllocationConfig config) override;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000114
mflodman101f2502016-06-09 17:21:19 +0200115 // Removes a previously added observer, but will not trigger a new bitrate
116 // allocation.
Sebastian Jansson83267802018-04-19 08:27:19 +0200117 void RemoveObserver(BitrateAllocatorObserver* observer) override;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000118
perkj57c21f92016-06-17 07:27:16 -0700119 // Returns initial bitrate allocated for |observer|. If |observer| is not in
120 // the list of added observers, a best guess is returned.
Sebastian Jansson83267802018-04-19 08:27:19 +0200121 int GetStartBitrate(BitrateAllocatorObserver* observer) override;
perkj57c21f92016-06-17 07:27:16 -0700122
Alex Narest78609d52017-10-20 10:37:47 +0200123 // Sets external allocation strategy. If strategy is not set default WebRTC
124 // allocation mechanism will be used. The strategy may be changed during call.
125 // Setting NULL value will restore default WEBRTC allocation strategy.
126 void SetBitrateAllocationStrategy(
127 std::unique_ptr<rtc::BitrateAllocationStrategy>
128 bitrate_allocation_strategy);
129
mflodman2ebe5b12016-05-13 01:43:51 -0700130 private:
Alex Narest78609d52017-10-20 10:37:47 +0200131 struct ObserverConfig : rtc::BitrateAllocationStrategy::TrackConfig {
mflodman2ebe5b12016-05-13 01:43:51 -0700132 ObserverConfig(BitrateAllocatorObserver* observer,
133 uint32_t min_bitrate_bps,
134 uint32_t max_bitrate_bps,
perkj71ee44c2016-06-15 00:47:53 -0700135 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200136 bool enforce_min_bitrate,
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800137 std::string track_id,
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100138 double bitrate_priority,
139 bool has_packet_feedback)
Alex Narest78609d52017-10-20 10:37:47 +0200140 : TrackConfig(min_bitrate_bps,
141 max_bitrate_bps,
142 enforce_min_bitrate,
143 track_id),
144 observer(observer),
perkj71ee44c2016-06-15 00:47:53 -0700145 pad_up_bitrate_bps(pad_up_bitrate_bps),
mflodman48a4beb2016-07-01 13:03:59 +0200146 allocated_bitrate_bps(-1),
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800147 media_ratio(1.0),
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100148 bitrate_priority(bitrate_priority),
149 has_packet_feedback(has_packet_feedback) {}
mflodman48a4beb2016-07-01 13:03:59 +0200150
151 BitrateAllocatorObserver* observer;
perkj71ee44c2016-06-15 00:47:53 -0700152 uint32_t pad_up_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200153 int64_t allocated_bitrate_bps;
154 double media_ratio; // Part of the total bitrate used for media [0.0, 1.0].
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800155 // The amount of bitrate allocated to this observer relative to all other
156 // observers. If an observer has twice the bitrate_priority of other
157 // observers, it should be allocated twice the bitrate above its min.
158 double bitrate_priority;
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100159 bool has_packet_feedback;
srte1eb051c2017-11-29 11:23:59 +0100160
161 uint32_t LastAllocatedBitrate() const;
162 // The minimum bitrate required by this observer, including
163 // enable-hysteresis if the observer is in a paused state.
164 uint32_t MinBitrateWithHysteresis() const;
mflodman2ebe5b12016-05-13 01:43:51 -0700165 };
166
perkj71ee44c2016-06-15 00:47:53 -0700167 // Calculates the minimum requested send bitrate and max padding bitrate and
168 // calls LimitObserver::OnAllocationLimitsChanged.
169 void UpdateAllocationLimits();
170
mflodman48a4beb2016-07-01 13:03:59 +0200171 typedef std::vector<ObserverConfig> ObserverConfigs;
172 ObserverConfigs::iterator FindObserverConfig(
perkj26091b12016-09-01 01:17:40 -0700173 const BitrateAllocatorObserver* observer);
mflodman2ebe5b12016-05-13 01:43:51 -0700174
175 typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap;
176 typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
177
perkj26091b12016-09-01 01:17:40 -0700178 ObserverAllocation AllocateBitrates(uint32_t bitrate);
Stefan Holmere5904162015-03-26 11:11:06 +0100179
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800180 // Allocates zero bitrate to all observers.
perkj26091b12016-09-01 01:17:40 -0700181 ObserverAllocation ZeroRateAllocation();
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800182 // Allocates bitrate to observers when there isn't enough to allocate the
183 // minimum to all observers.
perkj26091b12016-09-01 01:17:40 -0700184 ObserverAllocation LowRateAllocation(uint32_t bitrate);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800185 // Allocates bitrate to all observers when the available bandwidth is enough
186 // to allocate the minimum to all observers but not enough to allocate the
187 // max bitrate of each observer.
mflodman101f2502016-06-09 17:21:19 +0200188 ObserverAllocation NormalRateAllocation(uint32_t bitrate,
perkj26091b12016-09-01 01:17:40 -0700189 uint32_t sum_min_bitrates);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800190 // Allocates bitrate to observers when there is enough available bandwidth
191 // for all observers to be allocated their max bitrate.
mflodman101f2502016-06-09 17:21:19 +0200192 ObserverAllocation MaxRateAllocation(uint32_t bitrate,
perkj26091b12016-09-01 01:17:40 -0700193 uint32_t sum_max_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200194
mflodman101f2502016-06-09 17:21:19 +0200195 // Splits |bitrate| evenly to observers already in |allocation|.
196 // |include_zero_allocations| decides if zero allocations should be part of
197 // the distribution or not. The allowed max bitrate is |max_multiplier| x
198 // observer max bitrate.
199 void DistributeBitrateEvenly(uint32_t bitrate,
200 bool include_zero_allocations,
201 int max_multiplier,
perkj26091b12016-09-01 01:17:40 -0700202 ObserverAllocation* allocation);
203 bool EnoughBitrateForAllObservers(uint32_t bitrate,
204 uint32_t sum_min_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200205
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800206 // From the available |bitrate|, each observer will be allocated a
207 // proportional amount based upon its bitrate priority. If that amount is
208 // more than the observer's capacity, it will be allocated its capacity, and
209 // the excess bitrate is still allocated proportionally to other observers.
210 // Allocating the proportional amount means an observer with twice the
211 // bitrate_priority of another will be allocated twice the bitrate.
212 void DistributeBitrateRelatively(
213 uint32_t bitrate,
214 const ObserverAllocation& observers_capacities,
215 ObserverAllocation* allocation);
216
Ying Wanga646d302018-03-02 17:04:11 +0100217 // Allow packets to be transmitted in up to 2 times max video bitrate if the
218 // bandwidth estimate allows it.
219 // TODO(bugs.webrtc.org/8541): May be worth to refactor to keep this logic in
220 // video send stream. Similar logic is implemented in
221 // AudioPriorityBitrateAllocationStrategy.
222 uint8_t GetTransmissionMaxBitrateMultiplier();
223
perkj26091b12016-09-01 01:17:40 -0700224 rtc::SequencedTaskChecker sequenced_checker_;
danilchapa37de392017-09-09 04:17:22 -0700225 LimitObserver* const limit_observer_ RTC_GUARDED_BY(&sequenced_checker_);
Stefan Holmere5904162015-03-26 11:11:06 +0100226 // Stored in a list to keep track of the insertion order.
danilchapa37de392017-09-09 04:17:22 -0700227 ObserverConfigs bitrate_observer_configs_ RTC_GUARDED_BY(&sequenced_checker_);
228 uint32_t last_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
229 uint32_t last_non_zero_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
230 uint8_t last_fraction_loss_ RTC_GUARDED_BY(&sequenced_checker_);
231 int64_t last_rtt_ RTC_GUARDED_BY(&sequenced_checker_);
232 int64_t last_bwe_period_ms_ RTC_GUARDED_BY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200233 // Number of mute events based on too low BWE, not network up/down.
danilchapa37de392017-09-09 04:17:22 -0700234 int num_pause_events_ RTC_GUARDED_BY(&sequenced_checker_);
235 Clock* const clock_ RTC_GUARDED_BY(&sequenced_checker_);
236 int64_t last_bwe_log_time_ RTC_GUARDED_BY(&sequenced_checker_);
237 uint32_t total_requested_padding_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
238 uint32_t total_requested_min_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
Sebastian Jansson448f4d52018-04-04 14:52:07 +0200239 uint32_t total_requested_max_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100240 bool has_packet_feedback_ RTC_GUARDED_BY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200241 std::unique_ptr<rtc::BitrateAllocationStrategy> bitrate_allocation_strategy_
242 RTC_GUARDED_BY(&sequenced_checker_);
Ying Wanga646d302018-03-02 17:04:11 +0100243 uint8_t transmission_max_bitrate_multiplier_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000244};
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800245
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000246} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200247#endif // CALL_BITRATE_ALLOCATOR_H_