blob: 73258ad6d65be73970eea1909c3a5428c294de46 [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,
90 // TODO(shampson): Take out default value and wire the
91 // bitrate_priority up to the AudioSendStream::Config and
92 // VideoSendStream::Config.
93 double bitrate_priority = 1.0);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000094
mflodman101f2502016-06-09 17:21:19 +020095 // Removes a previously added observer, but will not trigger a new bitrate
96 // allocation.
mflodman86aabb22016-03-11 15:44:32 +010097 void RemoveObserver(BitrateAllocatorObserver* observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000098
perkj57c21f92016-06-17 07:27:16 -070099 // Returns initial bitrate allocated for |observer|. If |observer| is not in
100 // the list of added observers, a best guess is returned.
101 int GetStartBitrate(BitrateAllocatorObserver* observer);
102
Alex Narest78609d52017-10-20 10:37:47 +0200103 // Sets external allocation strategy. If strategy is not set default WebRTC
104 // allocation mechanism will be used. The strategy may be changed during call.
105 // Setting NULL value will restore default WEBRTC allocation strategy.
106 void SetBitrateAllocationStrategy(
107 std::unique_ptr<rtc::BitrateAllocationStrategy>
108 bitrate_allocation_strategy);
109
mflodman2ebe5b12016-05-13 01:43:51 -0700110 private:
Alex Narest78609d52017-10-20 10:37:47 +0200111 struct ObserverConfig : rtc::BitrateAllocationStrategy::TrackConfig {
mflodman2ebe5b12016-05-13 01:43:51 -0700112 ObserverConfig(BitrateAllocatorObserver* observer,
113 uint32_t min_bitrate_bps,
114 uint32_t max_bitrate_bps,
perkj71ee44c2016-06-15 00:47:53 -0700115 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200116 bool enforce_min_bitrate,
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800117 std::string track_id,
118 double bitrate_priority)
Alex Narest78609d52017-10-20 10:37:47 +0200119 : TrackConfig(min_bitrate_bps,
120 max_bitrate_bps,
121 enforce_min_bitrate,
122 track_id),
123 observer(observer),
perkj71ee44c2016-06-15 00:47:53 -0700124 pad_up_bitrate_bps(pad_up_bitrate_bps),
mflodman48a4beb2016-07-01 13:03:59 +0200125 allocated_bitrate_bps(-1),
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800126 media_ratio(1.0),
127 bitrate_priority(bitrate_priority) {}
mflodman48a4beb2016-07-01 13:03:59 +0200128
129 BitrateAllocatorObserver* observer;
perkj71ee44c2016-06-15 00:47:53 -0700130 uint32_t pad_up_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200131 int64_t allocated_bitrate_bps;
132 double media_ratio; // Part of the total bitrate used for media [0.0, 1.0].
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800133 // The amount of bitrate allocated to this observer relative to all other
134 // observers. If an observer has twice the bitrate_priority of other
135 // observers, it should be allocated twice the bitrate above its min.
136 double bitrate_priority;
mflodman2ebe5b12016-05-13 01:43:51 -0700137 };
138
perkj71ee44c2016-06-15 00:47:53 -0700139 // Calculates the minimum requested send bitrate and max padding bitrate and
140 // calls LimitObserver::OnAllocationLimitsChanged.
141 void UpdateAllocationLimits();
142
mflodman48a4beb2016-07-01 13:03:59 +0200143 typedef std::vector<ObserverConfig> ObserverConfigs;
144 ObserverConfigs::iterator FindObserverConfig(
perkj26091b12016-09-01 01:17:40 -0700145 const BitrateAllocatorObserver* observer);
mflodman2ebe5b12016-05-13 01:43:51 -0700146
147 typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap;
148 typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
149
perkj26091b12016-09-01 01:17:40 -0700150 ObserverAllocation AllocateBitrates(uint32_t bitrate);
Stefan Holmere5904162015-03-26 11:11:06 +0100151
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800152 // Allocates zero bitrate to all observers.
perkj26091b12016-09-01 01:17:40 -0700153 ObserverAllocation ZeroRateAllocation();
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800154 // Allocates bitrate to observers when there isn't enough to allocate the
155 // minimum to all observers.
perkj26091b12016-09-01 01:17:40 -0700156 ObserverAllocation LowRateAllocation(uint32_t bitrate);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800157 // Allocates bitrate to all observers when the available bandwidth is enough
158 // to allocate the minimum to all observers but not enough to allocate the
159 // max bitrate of each observer.
mflodman101f2502016-06-09 17:21:19 +0200160 ObserverAllocation NormalRateAllocation(uint32_t bitrate,
perkj26091b12016-09-01 01:17:40 -0700161 uint32_t sum_min_bitrates);
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800162 // Allocates bitrate to observers when there is enough available bandwidth
163 // for all observers to be allocated their max bitrate.
mflodman101f2502016-06-09 17:21:19 +0200164 ObserverAllocation MaxRateAllocation(uint32_t bitrate,
perkj26091b12016-09-01 01:17:40 -0700165 uint32_t sum_max_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200166
perkj26091b12016-09-01 01:17:40 -0700167 uint32_t LastAllocatedBitrate(const ObserverConfig& observer_config);
mflodman101f2502016-06-09 17:21:19 +0200168 // The minimum bitrate required by this observer, including enable-hysteresis
169 // if the observer is in a paused state.
perkj26091b12016-09-01 01:17:40 -0700170 uint32_t MinBitrateWithHysteresis(const ObserverConfig& observer_config);
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,
perkj26091b12016-09-01 01:17:40 -0700178 ObserverAllocation* allocation);
179 bool EnoughBitrateForAllObservers(uint32_t bitrate,
180 uint32_t sum_min_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200181
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800182 // From the available |bitrate|, each observer will be allocated a
183 // proportional amount based upon its bitrate priority. If that amount is
184 // more than the observer's capacity, it will be allocated its capacity, and
185 // the excess bitrate is still allocated proportionally to other observers.
186 // Allocating the proportional amount means an observer with twice the
187 // bitrate_priority of another will be allocated twice the bitrate.
188 void DistributeBitrateRelatively(
189 uint32_t bitrate,
190 const ObserverAllocation& observers_capacities,
191 ObserverAllocation* allocation);
192
perkj26091b12016-09-01 01:17:40 -0700193 rtc::SequencedTaskChecker sequenced_checker_;
danilchapa37de392017-09-09 04:17:22 -0700194 LimitObserver* const limit_observer_ RTC_GUARDED_BY(&sequenced_checker_);
Stefan Holmere5904162015-03-26 11:11:06 +0100195 // Stored in a list to keep track of the insertion order.
danilchapa37de392017-09-09 04:17:22 -0700196 ObserverConfigs bitrate_observer_configs_ RTC_GUARDED_BY(&sequenced_checker_);
197 uint32_t last_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
198 uint32_t last_non_zero_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
199 uint8_t last_fraction_loss_ RTC_GUARDED_BY(&sequenced_checker_);
200 int64_t last_rtt_ RTC_GUARDED_BY(&sequenced_checker_);
201 int64_t last_bwe_period_ms_ RTC_GUARDED_BY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200202 // Number of mute events based on too low BWE, not network up/down.
danilchapa37de392017-09-09 04:17:22 -0700203 int num_pause_events_ RTC_GUARDED_BY(&sequenced_checker_);
204 Clock* const clock_ RTC_GUARDED_BY(&sequenced_checker_);
205 int64_t last_bwe_log_time_ RTC_GUARDED_BY(&sequenced_checker_);
206 uint32_t total_requested_padding_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
207 uint32_t total_requested_min_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
Alex Narest78609d52017-10-20 10:37:47 +0200208 std::unique_ptr<rtc::BitrateAllocationStrategy> bitrate_allocation_strategy_
209 RTC_GUARDED_BY(&sequenced_checker_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000210};
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800211
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000212} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200213#endif // CALL_BITRATE_ALLOCATOR_H_