blob: 37e15b4f7b016c7315308e98302d90d030dfe32f [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
mflodman0e7e2592015-11-12 21:02:42 -080011#ifndef WEBRTC_CALL_BITRATE_ALLOCATOR_H_
12#define WEBRTC_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>
17#include <utility>
mflodman48a4beb2016-07-01 13:03:59 +020018#include <vector>
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000019
perkj8eb37a32016-08-16 02:40:55 -070020#include "webrtc/base/criticalsection.h"
21#include "webrtc/base/thread_annotations.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000022
23namespace webrtc {
24
mflodman48a4beb2016-07-01 13:03:59 +020025class Clock;
26
mflodman86aabb22016-03-11 15:44:32 +010027// Used by all send streams with adaptive bitrate, to get the currently
28// allocated bitrate for the send stream. The current network properties are
29// given at the same time, to let the send stream decide about possible loss
30// protection.
31class BitrateAllocatorObserver {
32 public:
mflodman48a4beb2016-07-01 13:03:59 +020033 // Returns the amount of protection used by the BitrateAllocatorObserver
34 // implementation, as bitrate in bps.
35 virtual uint32_t OnBitrateUpdated(uint32_t bitrate_bps,
36 uint8_t fraction_loss,
37 int64_t rtt) = 0;
perkj71ee44c2016-06-15 00:47:53 -070038 protected:
mflodman86aabb22016-03-11 15:44:32 +010039 virtual ~BitrateAllocatorObserver() {}
40};
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000041
mflodman86aabb22016-03-11 15:44:32 +010042// Usage: this class will register multiple RtcpBitrateObserver's one at each
43// RTCP module. It will aggregate the results and run one bandwidth estimation
44// and push the result to the encoders via BitrateAllocatorObserver(s).
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000045class BitrateAllocator {
46 public:
perkj71ee44c2016-06-15 00:47:53 -070047 // Used to get notified when send stream limits such as the minimum send
48 // bitrate and max padding bitrate is changed.
49 class LimitObserver {
50 public:
51 virtual void OnAllocationLimitsChanged(
52 uint32_t min_send_bitrate_bps,
53 uint32_t max_padding_bitrate_bps) = 0;
54
55 protected:
56 virtual ~LimitObserver() {}
57 };
58
59 explicit BitrateAllocator(LimitObserver* limit_observer);
mflodman48a4beb2016-07-01 13:03:59 +020060 ~BitrateAllocator();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000061
mflodman86aabb22016-03-11 15:44:32 +010062 // Allocate target_bitrate across the registered BitrateAllocatorObservers.
perkj71ee44c2016-06-15 00:47:53 -070063 void OnNetworkChanged(uint32_t target_bitrate_bps,
64 uint8_t fraction_loss,
65 int64_t rtt);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000066
67 // Set the start and max send bitrate used by the bandwidth management.
68 //
Peter Boström8e4e8b02015-09-15 15:08:03 +020069 // |observer| updates bitrates if already in use.
70 // |min_bitrate_bps| = 0 equals no min bitrate.
71 // |max_bitrate_bps| = 0 equals no max bitrate.
mflodman2ebe5b12016-05-13 01:43:51 -070072 // |enforce_min_bitrate| = 'true' will allocate at least |min_bitrate_bps| for
73 // this observer, even if the BWE is too low, 'false' will allocate 0 to
74 // the observer if BWE doesn't allow |min_bitrate_bps|.
perkjfea93092016-05-14 00:58:48 -070075 // Note that |observer|->OnBitrateUpdated() will be called within the scope of
76 // this method with the current rtt, fraction_loss and available bitrate and
77 // that the bitrate in OnBitrateUpdated will be zero if the |observer| is
78 // currently not allowed to send data.
perkj57c21f92016-06-17 07:27:16 -070079 void AddObserver(BitrateAllocatorObserver* observer,
80 uint32_t min_bitrate_bps,
81 uint32_t max_bitrate_bps,
82 uint32_t pad_up_bitrate_bps,
83 bool enforce_min_bitrate);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000084
mflodman101f2502016-06-09 17:21:19 +020085 // Removes a previously added observer, but will not trigger a new bitrate
86 // allocation.
mflodman86aabb22016-03-11 15:44:32 +010087 void RemoveObserver(BitrateAllocatorObserver* observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000088
perkj57c21f92016-06-17 07:27:16 -070089 // Returns initial bitrate allocated for |observer|. If |observer| is not in
90 // the list of added observers, a best guess is returned.
91 int GetStartBitrate(BitrateAllocatorObserver* observer);
92
mflodman2ebe5b12016-05-13 01:43:51 -070093 private:
mflodman101f2502016-06-09 17:21:19 +020094 // Note: All bitrates for member variables and methods are in bps.
mflodman2ebe5b12016-05-13 01:43:51 -070095 struct ObserverConfig {
96 ObserverConfig(BitrateAllocatorObserver* observer,
97 uint32_t min_bitrate_bps,
98 uint32_t max_bitrate_bps,
perkj71ee44c2016-06-15 00:47:53 -070099 uint32_t pad_up_bitrate_bps,
mflodman2ebe5b12016-05-13 01:43:51 -0700100 bool enforce_min_bitrate)
101 : observer(observer),
102 min_bitrate_bps(min_bitrate_bps),
103 max_bitrate_bps(max_bitrate_bps),
perkj71ee44c2016-06-15 00:47:53 -0700104 pad_up_bitrate_bps(pad_up_bitrate_bps),
mflodman48a4beb2016-07-01 13:03:59 +0200105 enforce_min_bitrate(enforce_min_bitrate),
106 allocated_bitrate_bps(-1),
107 media_ratio(1.0) {}
108
109 BitrateAllocatorObserver* observer;
mflodman2ebe5b12016-05-13 01:43:51 -0700110 uint32_t min_bitrate_bps;
111 uint32_t max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700112 uint32_t pad_up_bitrate_bps;
mflodman2ebe5b12016-05-13 01:43:51 -0700113 bool enforce_min_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200114 int64_t allocated_bitrate_bps;
115 double media_ratio; // Part of the total bitrate used for media [0.0, 1.0].
mflodman2ebe5b12016-05-13 01:43:51 -0700116 };
117
perkj71ee44c2016-06-15 00:47:53 -0700118 // Calculates the minimum requested send bitrate and max padding bitrate and
119 // calls LimitObserver::OnAllocationLimitsChanged.
120 void UpdateAllocationLimits();
121
mflodman48a4beb2016-07-01 13:03:59 +0200122 typedef std::vector<ObserverConfig> ObserverConfigs;
123 ObserverConfigs::iterator FindObserverConfig(
perkj8eb37a32016-08-16 02:40:55 -0700124 const BitrateAllocatorObserver* observer)
125 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
mflodman2ebe5b12016-05-13 01:43:51 -0700126
127 typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap;
128 typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
129
perkj8eb37a32016-08-16 02:40:55 -0700130 ObserverAllocation AllocateBitrates(uint32_t bitrate)
131 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100132
perkj8eb37a32016-08-16 02:40:55 -0700133 ObserverAllocation ZeroRateAllocation() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
134 ObserverAllocation LowRateAllocation(uint32_t bitrate)
135 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
mflodman101f2502016-06-09 17:21:19 +0200136 ObserverAllocation NormalRateAllocation(uint32_t bitrate,
perkj8eb37a32016-08-16 02:40:55 -0700137 uint32_t sum_min_bitrates)
138 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
mflodman101f2502016-06-09 17:21:19 +0200139 ObserverAllocation MaxRateAllocation(uint32_t bitrate,
perkj8eb37a32016-08-16 02:40:55 -0700140 uint32_t sum_max_bitrates)
141 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
mflodman101f2502016-06-09 17:21:19 +0200142
perkj8eb37a32016-08-16 02:40:55 -0700143 uint32_t LastAllocatedBitrate(const ObserverConfig& observer_config)
144 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
mflodman101f2502016-06-09 17:21:19 +0200145 // The minimum bitrate required by this observer, including enable-hysteresis
146 // if the observer is in a paused state.
perkj8eb37a32016-08-16 02:40:55 -0700147 uint32_t MinBitrateWithHysteresis(const ObserverConfig& observer_config)
148 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
mflodman101f2502016-06-09 17:21:19 +0200149 // Splits |bitrate| evenly to observers already in |allocation|.
150 // |include_zero_allocations| decides if zero allocations should be part of
151 // the distribution or not. The allowed max bitrate is |max_multiplier| x
152 // observer max bitrate.
153 void DistributeBitrateEvenly(uint32_t bitrate,
154 bool include_zero_allocations,
155 int max_multiplier,
perkj8eb37a32016-08-16 02:40:55 -0700156 ObserverAllocation* allocation)
157 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
158 bool EnoughBitrateForAllObservers(uint32_t bitrate, uint32_t sum_min_bitrates)
159 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
mflodman101f2502016-06-09 17:21:19 +0200160
perkj8eb37a32016-08-16 02:40:55 -0700161 LimitObserver* const limit_observer_;
162
163 rtc::CriticalSection crit_sect_;
Stefan Holmere5904162015-03-26 11:11:06 +0100164 // Stored in a list to keep track of the insertion order.
perkj8eb37a32016-08-16 02:40:55 -0700165 ObserverConfigs bitrate_observer_configs_ GUARDED_BY(crit_sect_);
166 uint32_t last_bitrate_bps_ GUARDED_BY(crit_sect_);
167 uint32_t last_non_zero_bitrate_bps_ GUARDED_BY(crit_sect_);
168 uint8_t last_fraction_loss_ GUARDED_BY(crit_sect_);
169 int64_t last_rtt_ GUARDED_BY(crit_sect_);
mflodman48a4beb2016-07-01 13:03:59 +0200170 // Number of mute events based on too low BWE, not network up/down.
perkj8eb37a32016-08-16 02:40:55 -0700171 int num_pause_events_ GUARDED_BY(crit_sect_);
172 Clock* const clock_;
173 int64_t last_bwe_log_time_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000174};
175} // namespace webrtc
mflodman0e7e2592015-11-12 21:02:42 -0800176#endif // WEBRTC_CALL_BITRATE_ALLOCATOR_H_