blob: fd9fe7172b7b46341f8b5bc2c40bf6a8b0084725 [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;
34 virtual ~BitrateAllocatorObserver() {}
35};
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000036
mflodman86aabb22016-03-11 15:44:32 +010037// Usage: this class will register multiple RtcpBitrateObserver's one at each
38// RTCP module. It will aggregate the results and run one bandwidth estimation
39// and push the result to the encoders via BitrateAllocatorObserver(s).
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000040class BitrateAllocator {
41 public:
42 BitrateAllocator();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000043
mflodman86aabb22016-03-11 15:44:32 +010044 // Allocate target_bitrate across the registered BitrateAllocatorObservers.
mflodman101f2502016-06-09 17:21:19 +020045 // Returns actual bitrate allocated, which might be higher than target_bitrate
46 // if for instance EnforceMinBitrate() is enabled.
47 uint32_t OnNetworkChanged(uint32_t target_bitrate_bps,
sprang2f48d942015-11-05 04:25:49 -080048 uint8_t fraction_loss,
49 int64_t rtt);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000050
51 // Set the start and max send bitrate used by the bandwidth management.
52 //
Peter Boström8e4e8b02015-09-15 15:08:03 +020053 // |observer| updates bitrates if already in use.
54 // |min_bitrate_bps| = 0 equals no min bitrate.
55 // |max_bitrate_bps| = 0 equals no max bitrate.
mflodman2ebe5b12016-05-13 01:43:51 -070056 // |enforce_min_bitrate| = 'true' will allocate at least |min_bitrate_bps| for
57 // this observer, even if the BWE is too low, 'false' will allocate 0 to
58 // the observer if BWE doesn't allow |min_bitrate_bps|.
perkjfea93092016-05-14 00:58:48 -070059 // Returns initial bitrate allocated for |observer|.
60 // Note that |observer|->OnBitrateUpdated() will be called within the scope of
61 // this method with the current rtt, fraction_loss and available bitrate and
62 // that the bitrate in OnBitrateUpdated will be zero if the |observer| is
63 // currently not allowed to send data.
mflodman86aabb22016-03-11 15:44:32 +010064 int AddObserver(BitrateAllocatorObserver* observer,
65 uint32_t min_bitrate_bps,
mflodman2ebe5b12016-05-13 01:43:51 -070066 uint32_t max_bitrate_bps,
67 bool enforce_min_bitrate);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000068
mflodman101f2502016-06-09 17:21:19 +020069 // Removes a previously added observer, but will not trigger a new bitrate
70 // allocation.
mflodman86aabb22016-03-11 15:44:32 +010071 void RemoveObserver(BitrateAllocatorObserver* observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000072
mflodman2ebe5b12016-05-13 01:43:51 -070073 private:
mflodman101f2502016-06-09 17:21:19 +020074 // Note: All bitrates for member variables and methods are in bps.
mflodman2ebe5b12016-05-13 01:43:51 -070075 struct ObserverConfig {
76 ObserverConfig(BitrateAllocatorObserver* observer,
77 uint32_t min_bitrate_bps,
78 uint32_t max_bitrate_bps,
79 bool enforce_min_bitrate)
80 : observer(observer),
81 min_bitrate_bps(min_bitrate_bps),
82 max_bitrate_bps(max_bitrate_bps),
83 enforce_min_bitrate(enforce_min_bitrate) {}
84 BitrateAllocatorObserver* const observer;
85 uint32_t min_bitrate_bps;
86 uint32_t max_bitrate_bps;
87 bool enforce_min_bitrate;
88 };
89
mflodman2ebe5b12016-05-13 01:43:51 -070090 typedef std::list<ObserverConfig> ObserverConfigList;
91 ObserverConfigList::iterator FindObserverConfig(
mflodman86aabb22016-03-11 15:44:32 +010092 const BitrateAllocatorObserver* observer)
93 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
mflodman2ebe5b12016-05-13 01:43:51 -070094
95 typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap;
96 typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
97
perkjfea93092016-05-14 00:58:48 -070098 ObserverAllocation AllocateBitrates(uint32_t bitrate)
99 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100100
mflodman2ebe5b12016-05-13 01:43:51 -0700101 ObserverAllocation ZeroRateAllocation() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
102 ObserverAllocation LowRateAllocation(uint32_t bitrate)
Stefan Holmere5904162015-03-26 11:11:06 +0100103 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
mflodman101f2502016-06-09 17:21:19 +0200104 ObserverAllocation NormalRateAllocation(uint32_t bitrate,
105 uint32_t sum_min_bitrates)
106 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
107 ObserverAllocation MaxRateAllocation(uint32_t bitrate,
108 uint32_t sum_max_bitrates)
109 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
110
111 uint32_t LastAllocatedBitrate(const ObserverConfig& observer_config)
112 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
113 // The minimum bitrate required by this observer, including enable-hysteresis
114 // if the observer is in a paused state.
115 uint32_t MinBitrateWithHysteresis(const ObserverConfig& observer_config)
116 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
117 // Splits |bitrate| evenly to observers already in |allocation|.
118 // |include_zero_allocations| decides if zero allocations should be part of
119 // the distribution or not. The allowed max bitrate is |max_multiplier| x
120 // observer max bitrate.
121 void DistributeBitrateEvenly(uint32_t bitrate,
122 bool include_zero_allocations,
123 int max_multiplier,
124 ObserverAllocation* allocation)
125 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
126 bool EnoughBitrateForAllObservers(uint32_t bitrate, uint32_t sum_min_bitrates)
127 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
128
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000129
pbos5ad935c2016-01-25 03:52:44 -0800130 rtc::CriticalSection crit_sect_;
Stefan Holmere5904162015-03-26 11:11:06 +0100131 // Stored in a list to keep track of the insertion order.
Peter Boström00b9d212016-05-19 16:59:03 +0200132 ObserverConfigList bitrate_observer_configs_ GUARDED_BY(crit_sect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100133 uint32_t last_bitrate_bps_ GUARDED_BY(crit_sect_);
perkjfea93092016-05-14 00:58:48 -0700134 uint32_t last_non_zero_bitrate_bps_ GUARDED_BY(crit_sect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100135 uint8_t last_fraction_loss_ GUARDED_BY(crit_sect_);
136 int64_t last_rtt_ GUARDED_BY(crit_sect_);
mflodman101f2502016-06-09 17:21:19 +0200137 ObserverAllocation last_allocation_ GUARDED_BY(crit_sect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000138};
139} // namespace webrtc
mflodman0e7e2592015-11-12 21:02:42 -0800140#endif // WEBRTC_CALL_BITRATE_ALLOCATOR_H_