blob: a5ed26c71b3bebf697d05f41d6e4190d7719bfa8 [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
perkj26091b12016-09-01 01:17:40 -070020#include "webrtc/base/sequenced_task_checker.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000021
22namespace webrtc {
23
mflodman48a4beb2016-07-01 13:03:59 +020024class Clock;
25
mflodman86aabb22016-03-11 15:44:32 +010026// Used by all send streams with adaptive bitrate, to get the currently
27// allocated bitrate for the send stream. The current network properties are
28// given at the same time, to let the send stream decide about possible loss
29// protection.
30class BitrateAllocatorObserver {
31 public:
mflodman48a4beb2016-07-01 13:03:59 +020032 // Returns the amount of protection used by the BitrateAllocatorObserver
33 // implementation, as bitrate in bps.
34 virtual uint32_t OnBitrateUpdated(uint32_t bitrate_bps,
35 uint8_t fraction_loss,
36 int64_t rtt) = 0;
perkj71ee44c2016-06-15 00:47:53 -070037 protected:
mflodman86aabb22016-03-11 15:44:32 +010038 virtual ~BitrateAllocatorObserver() {}
39};
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000040
mflodman86aabb22016-03-11 15:44:32 +010041// Usage: this class will register multiple RtcpBitrateObserver's one at each
42// RTCP module. It will aggregate the results and run one bandwidth estimation
43// and push the result to the encoders via BitrateAllocatorObserver(s).
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000044class BitrateAllocator {
45 public:
perkj71ee44c2016-06-15 00:47:53 -070046 // Used to get notified when send stream limits such as the minimum send
47 // bitrate and max padding bitrate is changed.
48 class LimitObserver {
49 public:
50 virtual void OnAllocationLimitsChanged(
51 uint32_t min_send_bitrate_bps,
52 uint32_t max_padding_bitrate_bps) = 0;
53
54 protected:
55 virtual ~LimitObserver() {}
56 };
57
58 explicit BitrateAllocator(LimitObserver* limit_observer);
mflodman48a4beb2016-07-01 13:03:59 +020059 ~BitrateAllocator();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000060
mflodman86aabb22016-03-11 15:44:32 +010061 // Allocate target_bitrate across the registered BitrateAllocatorObservers.
perkj71ee44c2016-06-15 00:47:53 -070062 void OnNetworkChanged(uint32_t target_bitrate_bps,
63 uint8_t fraction_loss,
64 int64_t rtt);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000065
66 // Set the start and max send bitrate used by the bandwidth management.
67 //
Peter Boström8e4e8b02015-09-15 15:08:03 +020068 // |observer| updates bitrates if already in use.
69 // |min_bitrate_bps| = 0 equals no min bitrate.
70 // |max_bitrate_bps| = 0 equals no max bitrate.
mflodman2ebe5b12016-05-13 01:43:51 -070071 // |enforce_min_bitrate| = 'true' will allocate at least |min_bitrate_bps| for
72 // this observer, even if the BWE is too low, 'false' will allocate 0 to
73 // the observer if BWE doesn't allow |min_bitrate_bps|.
perkjfea93092016-05-14 00:58:48 -070074 // Note that |observer|->OnBitrateUpdated() will be called within the scope of
75 // this method with the current rtt, fraction_loss and available bitrate and
76 // that the bitrate in OnBitrateUpdated will be zero if the |observer| is
77 // currently not allowed to send data.
perkj57c21f92016-06-17 07:27:16 -070078 void AddObserver(BitrateAllocatorObserver* observer,
79 uint32_t min_bitrate_bps,
80 uint32_t max_bitrate_bps,
81 uint32_t pad_up_bitrate_bps,
82 bool enforce_min_bitrate);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000083
mflodman101f2502016-06-09 17:21:19 +020084 // Removes a previously added observer, but will not trigger a new bitrate
85 // allocation.
mflodman86aabb22016-03-11 15:44:32 +010086 void RemoveObserver(BitrateAllocatorObserver* observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000087
perkj57c21f92016-06-17 07:27:16 -070088 // Returns initial bitrate allocated for |observer|. If |observer| is not in
89 // the list of added observers, a best guess is returned.
90 int GetStartBitrate(BitrateAllocatorObserver* observer);
91
mflodman2ebe5b12016-05-13 01:43:51 -070092 private:
mflodman101f2502016-06-09 17:21:19 +020093 // Note: All bitrates for member variables and methods are in bps.
mflodman2ebe5b12016-05-13 01:43:51 -070094 struct ObserverConfig {
95 ObserverConfig(BitrateAllocatorObserver* observer,
96 uint32_t min_bitrate_bps,
97 uint32_t max_bitrate_bps,
perkj71ee44c2016-06-15 00:47:53 -070098 uint32_t pad_up_bitrate_bps,
mflodman2ebe5b12016-05-13 01:43:51 -070099 bool enforce_min_bitrate)
100 : observer(observer),
101 min_bitrate_bps(min_bitrate_bps),
102 max_bitrate_bps(max_bitrate_bps),
perkj71ee44c2016-06-15 00:47:53 -0700103 pad_up_bitrate_bps(pad_up_bitrate_bps),
mflodman48a4beb2016-07-01 13:03:59 +0200104 enforce_min_bitrate(enforce_min_bitrate),
105 allocated_bitrate_bps(-1),
106 media_ratio(1.0) {}
107
108 BitrateAllocatorObserver* observer;
mflodman2ebe5b12016-05-13 01:43:51 -0700109 uint32_t min_bitrate_bps;
110 uint32_t max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700111 uint32_t pad_up_bitrate_bps;
mflodman2ebe5b12016-05-13 01:43:51 -0700112 bool enforce_min_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200113 int64_t allocated_bitrate_bps;
114 double media_ratio; // Part of the total bitrate used for media [0.0, 1.0].
mflodman2ebe5b12016-05-13 01:43:51 -0700115 };
116
perkj71ee44c2016-06-15 00:47:53 -0700117 // Calculates the minimum requested send bitrate and max padding bitrate and
118 // calls LimitObserver::OnAllocationLimitsChanged.
119 void UpdateAllocationLimits();
120
mflodman48a4beb2016-07-01 13:03:59 +0200121 typedef std::vector<ObserverConfig> ObserverConfigs;
122 ObserverConfigs::iterator FindObserverConfig(
perkj26091b12016-09-01 01:17:40 -0700123 const BitrateAllocatorObserver* observer);
mflodman2ebe5b12016-05-13 01:43:51 -0700124
125 typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap;
126 typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
127
perkj26091b12016-09-01 01:17:40 -0700128 ObserverAllocation AllocateBitrates(uint32_t bitrate);
Stefan Holmere5904162015-03-26 11:11:06 +0100129
perkj26091b12016-09-01 01:17:40 -0700130 ObserverAllocation ZeroRateAllocation();
131 ObserverAllocation LowRateAllocation(uint32_t bitrate);
mflodman101f2502016-06-09 17:21:19 +0200132 ObserverAllocation NormalRateAllocation(uint32_t bitrate,
perkj26091b12016-09-01 01:17:40 -0700133 uint32_t sum_min_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200134 ObserverAllocation MaxRateAllocation(uint32_t bitrate,
perkj26091b12016-09-01 01:17:40 -0700135 uint32_t sum_max_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200136
perkj26091b12016-09-01 01:17:40 -0700137 uint32_t LastAllocatedBitrate(const ObserverConfig& observer_config);
mflodman101f2502016-06-09 17:21:19 +0200138 // The minimum bitrate required by this observer, including enable-hysteresis
139 // if the observer is in a paused state.
perkj26091b12016-09-01 01:17:40 -0700140 uint32_t MinBitrateWithHysteresis(const ObserverConfig& observer_config);
mflodman101f2502016-06-09 17:21:19 +0200141 // Splits |bitrate| evenly to observers already in |allocation|.
142 // |include_zero_allocations| decides if zero allocations should be part of
143 // the distribution or not. The allowed max bitrate is |max_multiplier| x
144 // observer max bitrate.
145 void DistributeBitrateEvenly(uint32_t bitrate,
146 bool include_zero_allocations,
147 int max_multiplier,
perkj26091b12016-09-01 01:17:40 -0700148 ObserverAllocation* allocation);
149 bool EnoughBitrateForAllObservers(uint32_t bitrate,
150 uint32_t sum_min_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200151
perkj26091b12016-09-01 01:17:40 -0700152 rtc::SequencedTaskChecker sequenced_checker_;
153 LimitObserver* const limit_observer_ GUARDED_BY(&sequenced_checker_);
Stefan Holmere5904162015-03-26 11:11:06 +0100154 // Stored in a list to keep track of the insertion order.
perkj26091b12016-09-01 01:17:40 -0700155 ObserverConfigs bitrate_observer_configs_ GUARDED_BY(&sequenced_checker_);
156 uint32_t last_bitrate_bps_ GUARDED_BY(&sequenced_checker_);
157 uint32_t last_non_zero_bitrate_bps_ GUARDED_BY(&sequenced_checker_);
158 uint8_t last_fraction_loss_ GUARDED_BY(&sequenced_checker_);
159 int64_t last_rtt_ GUARDED_BY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200160 // Number of mute events based on too low BWE, not network up/down.
perkj26091b12016-09-01 01:17:40 -0700161 int num_pause_events_ GUARDED_BY(&sequenced_checker_);
162 Clock* const clock_ GUARDED_BY(&sequenced_checker_);
163 int64_t last_bwe_log_time_ GUARDED_BY(&sequenced_checker_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000164};
165} // namespace webrtc
mflodman0e7e2592015-11-12 21:02:42 -0800166#endif // WEBRTC_CALL_BITRATE_ALLOCATOR_H_