blob: 06e8b305d998a815504e12ecc76cdcaf042e9aaf [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 // Returns initial bitrate allocated for |observer|.
72 // Note that |observer|->OnBitrateUpdated() will be called within the scope of
73 // this method with the current rtt, fraction_loss and available bitrate and
74 // that the bitrate in OnBitrateUpdated will be zero if the |observer| is
75 // currently not allowed to send data.
mflodman86aabb22016-03-11 15:44:32 +010076 int AddObserver(BitrateAllocatorObserver* observer,
77 uint32_t min_bitrate_bps,
mflodman2ebe5b12016-05-13 01:43:51 -070078 uint32_t max_bitrate_bps,
perkj71ee44c2016-06-15 00:47:53 -070079 uint32_t pad_up_bitrate_bps,
mflodman2ebe5b12016-05-13 01:43:51 -070080 bool enforce_min_bitrate);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000081
mflodman101f2502016-06-09 17:21:19 +020082 // Removes a previously added observer, but will not trigger a new bitrate
83 // allocation.
mflodman86aabb22016-03-11 15:44:32 +010084 void RemoveObserver(BitrateAllocatorObserver* observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000085
mflodman2ebe5b12016-05-13 01:43:51 -070086 private:
mflodman101f2502016-06-09 17:21:19 +020087 // Note: All bitrates for member variables and methods are in bps.
mflodman2ebe5b12016-05-13 01:43:51 -070088 struct ObserverConfig {
89 ObserverConfig(BitrateAllocatorObserver* observer,
90 uint32_t min_bitrate_bps,
91 uint32_t max_bitrate_bps,
perkj71ee44c2016-06-15 00:47:53 -070092 uint32_t pad_up_bitrate_bps,
mflodman2ebe5b12016-05-13 01:43:51 -070093 bool enforce_min_bitrate)
94 : observer(observer),
95 min_bitrate_bps(min_bitrate_bps),
96 max_bitrate_bps(max_bitrate_bps),
perkj71ee44c2016-06-15 00:47:53 -070097 pad_up_bitrate_bps(pad_up_bitrate_bps),
mflodman2ebe5b12016-05-13 01:43:51 -070098 enforce_min_bitrate(enforce_min_bitrate) {}
99 BitrateAllocatorObserver* const observer;
100 uint32_t min_bitrate_bps;
101 uint32_t max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700102 uint32_t pad_up_bitrate_bps;
mflodman2ebe5b12016-05-13 01:43:51 -0700103 bool enforce_min_bitrate;
104 };
105
perkj71ee44c2016-06-15 00:47:53 -0700106 // Calculates the minimum requested send bitrate and max padding bitrate and
107 // calls LimitObserver::OnAllocationLimitsChanged.
108 void UpdateAllocationLimits();
109
mflodman2ebe5b12016-05-13 01:43:51 -0700110 typedef std::list<ObserverConfig> ObserverConfigList;
111 ObserverConfigList::iterator FindObserverConfig(
mflodman86aabb22016-03-11 15:44:32 +0100112 const BitrateAllocatorObserver* observer)
113 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
mflodman2ebe5b12016-05-13 01:43:51 -0700114
115 typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap;
116 typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
117
perkjfea93092016-05-14 00:58:48 -0700118 ObserverAllocation AllocateBitrates(uint32_t bitrate)
119 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100120
mflodman2ebe5b12016-05-13 01:43:51 -0700121 ObserverAllocation ZeroRateAllocation() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
122 ObserverAllocation LowRateAllocation(uint32_t bitrate)
Stefan Holmere5904162015-03-26 11:11:06 +0100123 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
mflodman101f2502016-06-09 17:21:19 +0200124 ObserverAllocation NormalRateAllocation(uint32_t bitrate,
125 uint32_t sum_min_bitrates)
126 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
127 ObserverAllocation MaxRateAllocation(uint32_t bitrate,
128 uint32_t sum_max_bitrates)
129 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
130
131 uint32_t LastAllocatedBitrate(const ObserverConfig& observer_config)
132 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
133 // The minimum bitrate required by this observer, including enable-hysteresis
134 // if the observer is in a paused state.
135 uint32_t MinBitrateWithHysteresis(const ObserverConfig& observer_config)
136 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
137 // Splits |bitrate| evenly to observers already in |allocation|.
138 // |include_zero_allocations| decides if zero allocations should be part of
139 // the distribution or not. The allowed max bitrate is |max_multiplier| x
140 // observer max bitrate.
141 void DistributeBitrateEvenly(uint32_t bitrate,
142 bool include_zero_allocations,
143 int max_multiplier,
144 ObserverAllocation* allocation)
145 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
146 bool EnoughBitrateForAllObservers(uint32_t bitrate, uint32_t sum_min_bitrates)
147 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
148
perkj71ee44c2016-06-15 00:47:53 -0700149 LimitObserver* const limit_observer_;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000150
pbos5ad935c2016-01-25 03:52:44 -0800151 rtc::CriticalSection crit_sect_;
Stefan Holmere5904162015-03-26 11:11:06 +0100152 // Stored in a list to keep track of the insertion order.
Peter Boström00b9d212016-05-19 16:59:03 +0200153 ObserverConfigList bitrate_observer_configs_ GUARDED_BY(crit_sect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100154 uint32_t last_bitrate_bps_ GUARDED_BY(crit_sect_);
perkjfea93092016-05-14 00:58:48 -0700155 uint32_t last_non_zero_bitrate_bps_ GUARDED_BY(crit_sect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100156 uint8_t last_fraction_loss_ GUARDED_BY(crit_sect_);
157 int64_t last_rtt_ GUARDED_BY(crit_sect_);
mflodman101f2502016-06-09 17:21:19 +0200158 ObserverAllocation last_allocation_ GUARDED_BY(crit_sect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000159};
160} // namespace webrtc
mflodman0e7e2592015-11-12 21:02:42 -0800161#endif // WEBRTC_CALL_BITRATE_ALLOCATOR_H_