blob: 18a2f4492376a0c6be0dcb29242ca27d143fe39b [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
mflodman86aabb22016-03-11 15:44:32 +010046// Usage: this class will register multiple RtcpBitrateObserver's one at each
47// RTCP module. It will aggregate the results and run one bandwidth estimation
48// and push the result to the encoders via BitrateAllocatorObserver(s).
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000049class BitrateAllocator {
50 public:
perkj71ee44c2016-06-15 00:47:53 -070051 // Used to get notified when send stream limits such as the minimum send
52 // bitrate and max padding bitrate is changed.
53 class LimitObserver {
54 public:
philipelf69e7682018-02-28 13:06:28 +010055 virtual void OnAllocationLimitsChanged(uint32_t min_send_bitrate_bps,
56 uint32_t max_padding_bitrate_bps,
Sebastian Janssonfe617a32018-03-21 12:45:20 +010057 uint32_t total_bitrate_bps,
58 bool has_packet_feedback) = 0;
perkj71ee44c2016-06-15 00:47:53 -070059
60 protected:
61 virtual ~LimitObserver() {}
62 };
63
64 explicit BitrateAllocator(LimitObserver* limit_observer);
mflodman48a4beb2016-07-01 13:03:59 +020065 ~BitrateAllocator();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000066
mflodman86aabb22016-03-11 15:44:32 +010067 // Allocate target_bitrate across the registered BitrateAllocatorObservers.
perkj71ee44c2016-06-15 00:47:53 -070068 void OnNetworkChanged(uint32_t target_bitrate_bps,
69 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080070 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070071 int64_t bwe_period_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000072
73 // Set the start and max send bitrate used by the bandwidth management.
74 //
Peter Boström8e4e8b02015-09-15 15:08:03 +020075 // |observer| updates bitrates if already in use.
76 // |min_bitrate_bps| = 0 equals no min bitrate.
77 // |max_bitrate_bps| = 0 equals no max bitrate.
mflodman2ebe5b12016-05-13 01:43:51 -070078 // |enforce_min_bitrate| = 'true' will allocate at least |min_bitrate_bps| for
79 // this observer, even if the BWE is too low, 'false' will allocate 0 to
80 // the observer if BWE doesn't allow |min_bitrate_bps|.
perkjfea93092016-05-14 00:58:48 -070081 // Note that |observer|->OnBitrateUpdated() will be called within the scope of
82 // this method with the current rtt, fraction_loss and available bitrate and
83 // that the bitrate in OnBitrateUpdated will be zero if the |observer| is
84 // currently not allowed to send data.
perkj57c21f92016-06-17 07:27:16 -070085 void AddObserver(BitrateAllocatorObserver* observer,
86 uint32_t min_bitrate_bps,
87 uint32_t max_bitrate_bps,
88 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +020089 bool enforce_min_bitrate,
Seth Hampsonfe73d6a2017-11-14 10:49:06 -080090 std::string track_id,
Seth Hampson24722b32017-12-22 09:36:42 -080091 double bitrate_priority);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000092
mflodman101f2502016-06-09 17:21:19 +020093 // Removes a previously added observer, but will not trigger a new bitrate
94 // allocation.
mflodman86aabb22016-03-11 15:44:32 +010095 void RemoveObserver(BitrateAllocatorObserver* observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000096
perkj57c21f92016-06-17 07:27:16 -070097 // Returns initial bitrate allocated for |observer|. If |observer| is not in
98 // the list of added observers, a best guess is returned.
99 int GetStartBitrate(BitrateAllocatorObserver* observer);
100
Alex Narest78609d52017-10-20 10:37:47 +0200101 // Sets external allocation strategy. If strategy is not set default WebRTC
102 // allocation mechanism will be used. The strategy may be changed during call.
103 // Setting NULL value will restore default WEBRTC allocation strategy.
104 void SetBitrateAllocationStrategy(
105 std::unique_ptr<rtc::BitrateAllocationStrategy>
106 bitrate_allocation_strategy);
107
mflodman2ebe5b12016-05-13 01:43:51 -0700108 private:
Alex Narest78609d52017-10-20 10:37:47 +0200109 struct ObserverConfig : rtc::BitrateAllocationStrategy::TrackConfig {
mflodman2ebe5b12016-05-13 01:43:51 -0700110 ObserverConfig(BitrateAllocatorObserver* observer,
111 uint32_t min_bitrate_bps,
112 uint32_t max_bitrate_bps,
perkj71ee44c2016-06-15 00:47:53 -0700113 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200114 bool enforce_min_bitrate,
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800115 std::string track_id,
116 double bitrate_priority)
Alex Narest78609d52017-10-20 10:37:47 +0200117 : TrackConfig(min_bitrate_bps,
118 max_bitrate_bps,
119 enforce_min_bitrate,
120 track_id),
121 observer(observer),
perkj71ee44c2016-06-15 00:47:53 -0700122 pad_up_bitrate_bps(pad_up_bitrate_bps),
mflodman48a4beb2016-07-01 13:03:59 +0200123 allocated_bitrate_bps(-1),
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800124 media_ratio(1.0),
125 bitrate_priority(bitrate_priority) {}
mflodman48a4beb2016-07-01 13:03:59 +0200126
127 BitrateAllocatorObserver* observer;
perkj71ee44c2016-06-15 00:47:53 -0700128 uint32_t pad_up_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200129 int64_t allocated_bitrate_bps;
130 double media_ratio; // Part of the total bitrate used for media [0.0, 1.0].
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800131 // The amount of bitrate allocated to this observer relative to all other
132 // observers. If an observer has twice the bitrate_priority of other
133 // observers, it should be allocated twice the bitrate above its min.
134 double bitrate_priority;
srte1eb051c2017-11-29 11:23:59 +0100135
136 uint32_t LastAllocatedBitrate() const;
137 // The minimum bitrate required by this observer, including
138 // enable-hysteresis if the observer is in a paused state.
139 uint32_t MinBitrateWithHysteresis() const;
mflodman2ebe5b12016-05-13 01:43:51 -0700140 };
141
perkj71ee44c2016-06-15 00:47:53 -0700142 // Calculates the minimum requested send bitrate and max padding bitrate and
143 // calls LimitObserver::OnAllocationLimitsChanged.
144 void UpdateAllocationLimits();
145
mflodman48a4beb2016-07-01 13:03:59 +0200146 typedef std::vector<ObserverConfig> ObserverConfigs;
147 ObserverConfigs::iterator FindObserverConfig(
perkj26091b12016-09-01 01:17:40 -0700148 const BitrateAllocatorObserver* observer);
mflodman2ebe5b12016-05-13 01:43:51 -0700149
150 typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap;
151 typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
152
perkj26091b12016-09-01 01:17:40 -0700153 ObserverAllocation AllocateBitrates(uint32_t bitrate);
Stefan Holmere5904162015-03-26 11:11:06 +0100154
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800155 // Allocates zero bitrate to all observers.
perkj26091b12016-09-01 01:17:40 -0700156 ObserverAllocation ZeroRateAllocation();
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800157 // Allocates bitrate to observers when there isn't enough to allocate the
158 // minimum to all observers.
perkj26091b12016-09-01 01:17:40 -0700159 ObserverAllocation LowRateAllocation(uint32_t bitrate);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800160 // Allocates bitrate to all observers when the available bandwidth is enough
161 // to allocate the minimum to all observers but not enough to allocate the
162 // max bitrate of each observer.
mflodman101f2502016-06-09 17:21:19 +0200163 ObserverAllocation NormalRateAllocation(uint32_t bitrate,
perkj26091b12016-09-01 01:17:40 -0700164 uint32_t sum_min_bitrates);
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,
perkj26091b12016-09-01 01:17:40 -0700168 uint32_t sum_max_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200169
mflodman101f2502016-06-09 17:21:19 +0200170 // Splits |bitrate| evenly to observers already in |allocation|.
171 // |include_zero_allocations| decides if zero allocations should be part of
172 // the distribution or not. The allowed max bitrate is |max_multiplier| x
173 // observer max bitrate.
174 void DistributeBitrateEvenly(uint32_t bitrate,
175 bool include_zero_allocations,
176 int max_multiplier,
perkj26091b12016-09-01 01:17:40 -0700177 ObserverAllocation* allocation);
178 bool EnoughBitrateForAllObservers(uint32_t bitrate,
179 uint32_t sum_min_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200180
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800181 // From the available |bitrate|, each observer will be allocated a
182 // proportional amount based upon its bitrate priority. If that amount is
183 // more than the observer's capacity, it will be allocated its capacity, and
184 // the excess bitrate is still allocated proportionally to other observers.
185 // Allocating the proportional amount means an observer with twice the
186 // bitrate_priority of another will be allocated twice the bitrate.
187 void DistributeBitrateRelatively(
188 uint32_t bitrate,
189 const ObserverAllocation& observers_capacities,
190 ObserverAllocation* allocation);
191
Ying Wanga646d302018-03-02 17:04:11 +0100192 // Allow packets to be transmitted in up to 2 times max video bitrate if the
193 // bandwidth estimate allows it.
194 // TODO(bugs.webrtc.org/8541): May be worth to refactor to keep this logic in
195 // video send stream. Similar logic is implemented in
196 // AudioPriorityBitrateAllocationStrategy.
197 uint8_t GetTransmissionMaxBitrateMultiplier();
198
perkj26091b12016-09-01 01:17:40 -0700199 rtc::SequencedTaskChecker sequenced_checker_;
danilchapa37de392017-09-09 04:17:22 -0700200 LimitObserver* const limit_observer_ RTC_GUARDED_BY(&sequenced_checker_);
Stefan Holmere5904162015-03-26 11:11:06 +0100201 // Stored in a list to keep track of the insertion order.
danilchapa37de392017-09-09 04:17:22 -0700202 ObserverConfigs bitrate_observer_configs_ RTC_GUARDED_BY(&sequenced_checker_);
203 uint32_t last_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
204 uint32_t last_non_zero_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
205 uint8_t last_fraction_loss_ RTC_GUARDED_BY(&sequenced_checker_);
206 int64_t last_rtt_ RTC_GUARDED_BY(&sequenced_checker_);
207 int64_t last_bwe_period_ms_ RTC_GUARDED_BY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200208 // Number of mute events based on too low BWE, not network up/down.
danilchapa37de392017-09-09 04:17:22 -0700209 int num_pause_events_ RTC_GUARDED_BY(&sequenced_checker_);
210 Clock* const clock_ RTC_GUARDED_BY(&sequenced_checker_);
211 int64_t last_bwe_log_time_ RTC_GUARDED_BY(&sequenced_checker_);
212 uint32_t total_requested_padding_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
213 uint32_t total_requested_min_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200214 std::unique_ptr<rtc::BitrateAllocationStrategy> bitrate_allocation_strategy_
215 RTC_GUARDED_BY(&sequenced_checker_);
Ying Wanga646d302018-03-02 17:04:11 +0100216 uint8_t transmission_max_bitrate_multiplier_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000217};
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800218
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000219} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200220#endif // CALL_BITRATE_ALLOCATOR_H_