blob: 58d1c72fb97b8bbb40884ac651d7038fde7a4e0a [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 <list>
17#include <map>
18#include <utility>
19
tommi63cb4342016-01-20 02:32:54 -080020#include "webrtc/base/criticalsection.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000021#include "webrtc/base/thread_annotations.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000022
23namespace webrtc {
24
mflodman86aabb22016-03-11 15:44:32 +010025// Used by all send streams with adaptive bitrate, to get the currently
26// allocated bitrate for the send stream. The current network properties are
27// given at the same time, to let the send stream decide about possible loss
28// protection.
29class BitrateAllocatorObserver {
30 public:
31 virtual void OnBitrateUpdated(uint32_t bitrate_bps,
32 uint8_t fraction_loss,
33 int64_t rtt) = 0;
perkj71ee44c2016-06-15 00:47:53 -070034
35 protected:
mflodman86aabb22016-03-11 15:44:32 +010036 virtual ~BitrateAllocatorObserver() {}
37};
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000038
mflodman86aabb22016-03-11 15:44:32 +010039// Usage: this class will register multiple RtcpBitrateObserver's one at each
40// RTCP module. It will aggregate the results and run one bandwidth estimation
41// and push the result to the encoders via BitrateAllocatorObserver(s).
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000042class BitrateAllocator {
43 public:
perkj71ee44c2016-06-15 00:47:53 -070044 // Used to get notified when send stream limits such as the minimum send
45 // bitrate and max padding bitrate is changed.
46 class LimitObserver {
47 public:
48 virtual void OnAllocationLimitsChanged(
49 uint32_t min_send_bitrate_bps,
50 uint32_t max_padding_bitrate_bps) = 0;
51
52 protected:
53 virtual ~LimitObserver() {}
54 };
55
56 explicit BitrateAllocator(LimitObserver* limit_observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000057
mflodman86aabb22016-03-11 15:44:32 +010058 // Allocate target_bitrate across the registered BitrateAllocatorObservers.
perkj71ee44c2016-06-15 00:47:53 -070059 void OnNetworkChanged(uint32_t target_bitrate_bps,
60 uint8_t fraction_loss,
61 int64_t rtt);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000062
63 // Set the start and max send bitrate used by the bandwidth management.
64 //
Peter Boström8e4e8b02015-09-15 15:08:03 +020065 // |observer| updates bitrates if already in use.
66 // |min_bitrate_bps| = 0 equals no min bitrate.
67 // |max_bitrate_bps| = 0 equals no max bitrate.
mflodman2ebe5b12016-05-13 01:43:51 -070068 // |enforce_min_bitrate| = 'true' will allocate at least |min_bitrate_bps| for
69 // this observer, even if the BWE is too low, 'false' will allocate 0 to
70 // the observer if BWE doesn't allow |min_bitrate_bps|.
perkjfea93092016-05-14 00:58:48 -070071 // Note that |observer|->OnBitrateUpdated() will be called within the scope of
72 // this method with the current rtt, fraction_loss and available bitrate and
73 // that the bitrate in OnBitrateUpdated will be zero if the |observer| is
74 // currently not allowed to send data.
perkj57c21f92016-06-17 07:27:16 -070075 void AddObserver(BitrateAllocatorObserver* observer,
76 uint32_t min_bitrate_bps,
77 uint32_t max_bitrate_bps,
78 uint32_t pad_up_bitrate_bps,
79 bool enforce_min_bitrate);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000080
mflodman101f2502016-06-09 17:21:19 +020081 // Removes a previously added observer, but will not trigger a new bitrate
82 // allocation.
mflodman86aabb22016-03-11 15:44:32 +010083 void RemoveObserver(BitrateAllocatorObserver* observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000084
perkj57c21f92016-06-17 07:27:16 -070085 // Returns initial bitrate allocated for |observer|. If |observer| is not in
86 // the list of added observers, a best guess is returned.
87 int GetStartBitrate(BitrateAllocatorObserver* observer);
88
mflodman2ebe5b12016-05-13 01:43:51 -070089 private:
mflodman101f2502016-06-09 17:21:19 +020090 // Note: All bitrates for member variables and methods are in bps.
mflodman2ebe5b12016-05-13 01:43:51 -070091 struct ObserverConfig {
92 ObserverConfig(BitrateAllocatorObserver* observer,
93 uint32_t min_bitrate_bps,
94 uint32_t max_bitrate_bps,
perkj71ee44c2016-06-15 00:47:53 -070095 uint32_t pad_up_bitrate_bps,
mflodman2ebe5b12016-05-13 01:43:51 -070096 bool enforce_min_bitrate)
97 : observer(observer),
98 min_bitrate_bps(min_bitrate_bps),
99 max_bitrate_bps(max_bitrate_bps),
perkj71ee44c2016-06-15 00:47:53 -0700100 pad_up_bitrate_bps(pad_up_bitrate_bps),
mflodman2ebe5b12016-05-13 01:43:51 -0700101 enforce_min_bitrate(enforce_min_bitrate) {}
102 BitrateAllocatorObserver* const observer;
103 uint32_t min_bitrate_bps;
104 uint32_t max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700105 uint32_t pad_up_bitrate_bps;
mflodman2ebe5b12016-05-13 01:43:51 -0700106 bool enforce_min_bitrate;
107 };
108
perkj71ee44c2016-06-15 00:47:53 -0700109 // Calculates the minimum requested send bitrate and max padding bitrate and
110 // calls LimitObserver::OnAllocationLimitsChanged.
111 void UpdateAllocationLimits();
112
mflodman2ebe5b12016-05-13 01:43:51 -0700113 typedef std::list<ObserverConfig> ObserverConfigList;
114 ObserverConfigList::iterator FindObserverConfig(
mflodman86aabb22016-03-11 15:44:32 +0100115 const BitrateAllocatorObserver* observer)
116 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
mflodman2ebe5b12016-05-13 01:43:51 -0700117
118 typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap;
119 typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
120
perkjfea93092016-05-14 00:58:48 -0700121 ObserverAllocation AllocateBitrates(uint32_t bitrate)
122 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100123
mflodman2ebe5b12016-05-13 01:43:51 -0700124 ObserverAllocation ZeroRateAllocation() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
125 ObserverAllocation LowRateAllocation(uint32_t bitrate)
Stefan Holmere5904162015-03-26 11:11:06 +0100126 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
mflodman101f2502016-06-09 17:21:19 +0200127 ObserverAllocation NormalRateAllocation(uint32_t bitrate,
128 uint32_t sum_min_bitrates)
129 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
130 ObserverAllocation MaxRateAllocation(uint32_t bitrate,
131 uint32_t sum_max_bitrates)
132 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
133
134 uint32_t LastAllocatedBitrate(const ObserverConfig& observer_config)
135 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
136 // The minimum bitrate required by this observer, including enable-hysteresis
137 // if the observer is in a paused state.
138 uint32_t MinBitrateWithHysteresis(const ObserverConfig& observer_config)
139 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
140 // Splits |bitrate| evenly to observers already in |allocation|.
141 // |include_zero_allocations| decides if zero allocations should be part of
142 // the distribution or not. The allowed max bitrate is |max_multiplier| x
143 // observer max bitrate.
144 void DistributeBitrateEvenly(uint32_t bitrate,
145 bool include_zero_allocations,
146 int max_multiplier,
147 ObserverAllocation* allocation)
148 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
149 bool EnoughBitrateForAllObservers(uint32_t bitrate, uint32_t sum_min_bitrates)
150 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
151
perkj71ee44c2016-06-15 00:47:53 -0700152 LimitObserver* const limit_observer_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000153
pbos5ad935c2016-01-25 03:52:44 -0800154 rtc::CriticalSection crit_sect_;
Stefan Holmere5904162015-03-26 11:11:06 +0100155 // Stored in a list to keep track of the insertion order.
Peter Boström00b9d212016-05-19 16:59:03 +0200156 ObserverConfigList bitrate_observer_configs_ GUARDED_BY(crit_sect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100157 uint32_t last_bitrate_bps_ GUARDED_BY(crit_sect_);
perkjfea93092016-05-14 00:58:48 -0700158 uint32_t last_non_zero_bitrate_bps_ GUARDED_BY(crit_sect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100159 uint8_t last_fraction_loss_ GUARDED_BY(crit_sect_);
160 int64_t last_rtt_ GUARDED_BY(crit_sect_);
mflodman101f2502016-06-09 17:21:19 +0200161 ObserverAllocation last_allocation_ GUARDED_BY(crit_sect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000162};
163} // namespace webrtc
mflodman0e7e2592015-11-12 21:02:42 -0800164#endif // WEBRTC_CALL_BITRATE_ALLOCATOR_H_