blob: b1afc8c424ebfa9f9f8348998636f239ce264e6e [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:
55 virtual void OnAllocationLimitsChanged(
56 uint32_t min_send_bitrate_bps,
57 uint32_t max_padding_bitrate_bps) = 0;
58
59 protected:
60 virtual ~LimitObserver() {}
61 };
62
63 explicit BitrateAllocator(LimitObserver* limit_observer);
mflodman48a4beb2016-07-01 13:03:59 +020064 ~BitrateAllocator();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000065
mflodman86aabb22016-03-11 15:44:32 +010066 // Allocate target_bitrate across the registered BitrateAllocatorObservers.
perkj71ee44c2016-06-15 00:47:53 -070067 void OnNetworkChanged(uint32_t target_bitrate_bps,
68 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080069 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070070 int64_t bwe_period_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000071
72 // Set the start and max send bitrate used by the bandwidth management.
73 //
Peter Boström8e4e8b02015-09-15 15:08:03 +020074 // |observer| updates bitrates if already in use.
75 // |min_bitrate_bps| = 0 equals no min bitrate.
76 // |max_bitrate_bps| = 0 equals no max bitrate.
mflodman2ebe5b12016-05-13 01:43:51 -070077 // |enforce_min_bitrate| = 'true' will allocate at least |min_bitrate_bps| for
78 // this observer, even if the BWE is too low, 'false' will allocate 0 to
79 // the observer if BWE doesn't allow |min_bitrate_bps|.
perkjfea93092016-05-14 00:58:48 -070080 // Note that |observer|->OnBitrateUpdated() will be called within the scope of
81 // this method with the current rtt, fraction_loss and available bitrate and
82 // that the bitrate in OnBitrateUpdated will be zero if the |observer| is
83 // currently not allowed to send data.
perkj57c21f92016-06-17 07:27:16 -070084 void AddObserver(BitrateAllocatorObserver* observer,
85 uint32_t min_bitrate_bps,
86 uint32_t max_bitrate_bps,
87 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +020088 bool enforce_min_bitrate,
Seth Hampsonfe73d6a2017-11-14 10:49:06 -080089 std::string track_id,
Seth Hampson24722b32017-12-22 09:36:42 -080090 double bitrate_priority);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000091
mflodman101f2502016-06-09 17:21:19 +020092 // Removes a previously added observer, but will not trigger a new bitrate
93 // allocation.
mflodman86aabb22016-03-11 15:44:32 +010094 void RemoveObserver(BitrateAllocatorObserver* observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000095
perkj57c21f92016-06-17 07:27:16 -070096 // Returns initial bitrate allocated for |observer|. If |observer| is not in
97 // the list of added observers, a best guess is returned.
98 int GetStartBitrate(BitrateAllocatorObserver* observer);
99
Alex Narest78609d52017-10-20 10:37:47 +0200100 // Sets external allocation strategy. If strategy is not set default WebRTC
101 // allocation mechanism will be used. The strategy may be changed during call.
102 // Setting NULL value will restore default WEBRTC allocation strategy.
103 void SetBitrateAllocationStrategy(
104 std::unique_ptr<rtc::BitrateAllocationStrategy>
105 bitrate_allocation_strategy);
106
mflodman2ebe5b12016-05-13 01:43:51 -0700107 private:
Alex Narest78609d52017-10-20 10:37:47 +0200108 struct ObserverConfig : rtc::BitrateAllocationStrategy::TrackConfig {
mflodman2ebe5b12016-05-13 01:43:51 -0700109 ObserverConfig(BitrateAllocatorObserver* observer,
110 uint32_t min_bitrate_bps,
111 uint32_t max_bitrate_bps,
perkj71ee44c2016-06-15 00:47:53 -0700112 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200113 bool enforce_min_bitrate,
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800114 std::string track_id,
115 double bitrate_priority)
Alex Narest78609d52017-10-20 10:37:47 +0200116 : TrackConfig(min_bitrate_bps,
117 max_bitrate_bps,
118 enforce_min_bitrate,
119 track_id),
120 observer(observer),
perkj71ee44c2016-06-15 00:47:53 -0700121 pad_up_bitrate_bps(pad_up_bitrate_bps),
mflodman48a4beb2016-07-01 13:03:59 +0200122 allocated_bitrate_bps(-1),
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800123 media_ratio(1.0),
124 bitrate_priority(bitrate_priority) {}
mflodman48a4beb2016-07-01 13:03:59 +0200125
126 BitrateAllocatorObserver* observer;
perkj71ee44c2016-06-15 00:47:53 -0700127 uint32_t pad_up_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200128 int64_t allocated_bitrate_bps;
129 double media_ratio; // Part of the total bitrate used for media [0.0, 1.0].
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800130 // The amount of bitrate allocated to this observer relative to all other
131 // observers. If an observer has twice the bitrate_priority of other
132 // observers, it should be allocated twice the bitrate above its min.
133 double bitrate_priority;
srte1eb051c2017-11-29 11:23:59 +0100134
135 uint32_t LastAllocatedBitrate() const;
136 // The minimum bitrate required by this observer, including
137 // enable-hysteresis if the observer is in a paused state.
138 uint32_t MinBitrateWithHysteresis() const;
mflodman2ebe5b12016-05-13 01:43:51 -0700139 };
140
perkj71ee44c2016-06-15 00:47:53 -0700141 // Calculates the minimum requested send bitrate and max padding bitrate and
142 // calls LimitObserver::OnAllocationLimitsChanged.
143 void UpdateAllocationLimits();
144
mflodman48a4beb2016-07-01 13:03:59 +0200145 typedef std::vector<ObserverConfig> ObserverConfigs;
146 ObserverConfigs::iterator FindObserverConfig(
perkj26091b12016-09-01 01:17:40 -0700147 const BitrateAllocatorObserver* observer);
mflodman2ebe5b12016-05-13 01:43:51 -0700148
149 typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap;
150 typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
151
perkj26091b12016-09-01 01:17:40 -0700152 ObserverAllocation AllocateBitrates(uint32_t bitrate);
Stefan Holmere5904162015-03-26 11:11:06 +0100153
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800154 // Allocates zero bitrate to all observers.
perkj26091b12016-09-01 01:17:40 -0700155 ObserverAllocation ZeroRateAllocation();
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800156 // Allocates bitrate to observers when there isn't enough to allocate the
157 // minimum to all observers.
perkj26091b12016-09-01 01:17:40 -0700158 ObserverAllocation LowRateAllocation(uint32_t bitrate);
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,
perkj26091b12016-09-01 01:17:40 -0700163 uint32_t sum_min_bitrates);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800164 // Allocates bitrate to observers when there is enough available bandwidth
165 // for all observers to be allocated their max bitrate.
mflodman101f2502016-06-09 17:21:19 +0200166 ObserverAllocation MaxRateAllocation(uint32_t bitrate,
perkj26091b12016-09-01 01:17:40 -0700167 uint32_t sum_max_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200168
mflodman101f2502016-06-09 17:21:19 +0200169 // Splits |bitrate| evenly to observers already in |allocation|.
170 // |include_zero_allocations| decides if zero allocations should be part of
171 // the distribution or not. The allowed max bitrate is |max_multiplier| x
172 // observer max bitrate.
173 void DistributeBitrateEvenly(uint32_t bitrate,
174 bool include_zero_allocations,
175 int max_multiplier,
perkj26091b12016-09-01 01:17:40 -0700176 ObserverAllocation* allocation);
177 bool EnoughBitrateForAllObservers(uint32_t bitrate,
178 uint32_t sum_min_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200179
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800180 // From the available |bitrate|, each observer will be allocated a
181 // proportional amount based upon its bitrate priority. If that amount is
182 // more than the observer's capacity, it will be allocated its capacity, and
183 // the excess bitrate is still allocated proportionally to other observers.
184 // Allocating the proportional amount means an observer with twice the
185 // bitrate_priority of another will be allocated twice the bitrate.
186 void DistributeBitrateRelatively(
187 uint32_t bitrate,
188 const ObserverAllocation& observers_capacities,
189 ObserverAllocation* allocation);
190
perkj26091b12016-09-01 01:17:40 -0700191 rtc::SequencedTaskChecker sequenced_checker_;
danilchapa37de392017-09-09 04:17:22 -0700192 LimitObserver* const limit_observer_ RTC_GUARDED_BY(&sequenced_checker_);
Stefan Holmere5904162015-03-26 11:11:06 +0100193 // Stored in a list to keep track of the insertion order.
danilchapa37de392017-09-09 04:17:22 -0700194 ObserverConfigs bitrate_observer_configs_ RTC_GUARDED_BY(&sequenced_checker_);
195 uint32_t last_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
196 uint32_t last_non_zero_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
197 uint8_t last_fraction_loss_ RTC_GUARDED_BY(&sequenced_checker_);
198 int64_t last_rtt_ RTC_GUARDED_BY(&sequenced_checker_);
199 int64_t last_bwe_period_ms_ RTC_GUARDED_BY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200200 // Number of mute events based on too low BWE, not network up/down.
danilchapa37de392017-09-09 04:17:22 -0700201 int num_pause_events_ RTC_GUARDED_BY(&sequenced_checker_);
202 Clock* const clock_ RTC_GUARDED_BY(&sequenced_checker_);
203 int64_t last_bwe_log_time_ RTC_GUARDED_BY(&sequenced_checker_);
204 uint32_t total_requested_padding_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
205 uint32_t total_requested_min_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200206 std::unique_ptr<rtc::BitrateAllocationStrategy> bitrate_allocation_strategy_
207 RTC_GUARDED_BY(&sequenced_checker_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000208};
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800209
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000210} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200211#endif // CALL_BITRATE_ALLOCATOR_H_