blob: d51740cc1b20591504c06b252f5ef6fab6e335af [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef CALL_BITRATE_ALLOCATOR_H_
12#define 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>
Alex Narestb3944f02017-10-13 14:56:18 +020017#include <string>
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000018#include <utility>
mflodman48a4beb2016-07-01 13:03:59 +020019#include <vector>
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000020
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/sequenced_task_checker.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,
minyue78b4d562016-11-30 04:47:39 -080037 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070038 int64_t bwe_period_ms) = 0;
minyue78b4d562016-11-30 04:47:39 -080039
perkj71ee44c2016-06-15 00:47:53 -070040 protected:
mflodman86aabb22016-03-11 15:44:32 +010041 virtual ~BitrateAllocatorObserver() {}
42};
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000043
mflodman86aabb22016-03-11 15:44:32 +010044// Usage: this class will register multiple RtcpBitrateObserver's one at each
45// RTCP module. It will aggregate the results and run one bandwidth estimation
46// and push the result to the encoders via BitrateAllocatorObserver(s).
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000047class BitrateAllocator {
48 public:
perkj71ee44c2016-06-15 00:47:53 -070049 // Used to get notified when send stream limits such as the minimum send
50 // bitrate and max padding bitrate is changed.
51 class LimitObserver {
52 public:
53 virtual void OnAllocationLimitsChanged(
54 uint32_t min_send_bitrate_bps,
55 uint32_t max_padding_bitrate_bps) = 0;
56
57 protected:
58 virtual ~LimitObserver() {}
59 };
60
61 explicit BitrateAllocator(LimitObserver* limit_observer);
mflodman48a4beb2016-07-01 13:03:59 +020062 ~BitrateAllocator();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000063
mflodman86aabb22016-03-11 15:44:32 +010064 // Allocate target_bitrate across the registered BitrateAllocatorObservers.
perkj71ee44c2016-06-15 00:47:53 -070065 void OnNetworkChanged(uint32_t target_bitrate_bps,
66 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080067 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070068 int64_t bwe_period_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000069
70 // Set the start and max send bitrate used by the bandwidth management.
71 //
Peter Boström8e4e8b02015-09-15 15:08:03 +020072 // |observer| updates bitrates if already in use.
73 // |min_bitrate_bps| = 0 equals no min bitrate.
74 // |max_bitrate_bps| = 0 equals no max bitrate.
mflodman2ebe5b12016-05-13 01:43:51 -070075 // |enforce_min_bitrate| = 'true' will allocate at least |min_bitrate_bps| for
76 // this observer, even if the BWE is too low, 'false' will allocate 0 to
77 // the observer if BWE doesn't allow |min_bitrate_bps|.
perkjfea93092016-05-14 00:58:48 -070078 // Note that |observer|->OnBitrateUpdated() will be called within the scope of
79 // this method with the current rtt, fraction_loss and available bitrate and
80 // that the bitrate in OnBitrateUpdated will be zero if the |observer| is
81 // currently not allowed to send data.
perkj57c21f92016-06-17 07:27:16 -070082 void AddObserver(BitrateAllocatorObserver* observer,
83 uint32_t min_bitrate_bps,
84 uint32_t max_bitrate_bps,
85 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +020086 bool enforce_min_bitrate,
87 std::string track_id);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000088
mflodman101f2502016-06-09 17:21:19 +020089 // Removes a previously added observer, but will not trigger a new bitrate
90 // allocation.
mflodman86aabb22016-03-11 15:44:32 +010091 void RemoveObserver(BitrateAllocatorObserver* observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000092
perkj57c21f92016-06-17 07:27:16 -070093 // Returns initial bitrate allocated for |observer|. If |observer| is not in
94 // the list of added observers, a best guess is returned.
95 int GetStartBitrate(BitrateAllocatorObserver* observer);
96
mflodman2ebe5b12016-05-13 01:43:51 -070097 private:
mflodman101f2502016-06-09 17:21:19 +020098 // Note: All bitrates for member variables and methods are in bps.
Lu Liu39260c42017-10-17 19:58:53 +000099 struct ObserverConfig {
mflodman2ebe5b12016-05-13 01:43:51 -0700100 ObserverConfig(BitrateAllocatorObserver* observer,
101 uint32_t min_bitrate_bps,
102 uint32_t max_bitrate_bps,
perkj71ee44c2016-06-15 00:47:53 -0700103 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200104 bool enforce_min_bitrate,
105 std::string track_id)
Lu Liu39260c42017-10-17 19:58:53 +0000106 : observer(observer),
107 min_bitrate_bps(min_bitrate_bps),
108 max_bitrate_bps(max_bitrate_bps),
perkj71ee44c2016-06-15 00:47:53 -0700109 pad_up_bitrate_bps(pad_up_bitrate_bps),
Lu Liu39260c42017-10-17 19:58:53 +0000110 enforce_min_bitrate(enforce_min_bitrate),
mflodman48a4beb2016-07-01 13:03:59 +0200111 allocated_bitrate_bps(-1),
Lu Liu39260c42017-10-17 19:58:53 +0000112 media_ratio(1.0),
113 track_id(track_id) {}
mflodman48a4beb2016-07-01 13:03:59 +0200114
115 BitrateAllocatorObserver* observer;
Lu Liu39260c42017-10-17 19:58:53 +0000116 uint32_t min_bitrate_bps;
117 uint32_t max_bitrate_bps;
perkj71ee44c2016-06-15 00:47:53 -0700118 uint32_t pad_up_bitrate_bps;
Lu Liu39260c42017-10-17 19:58:53 +0000119 bool enforce_min_bitrate;
mflodman48a4beb2016-07-01 13:03:59 +0200120 int64_t allocated_bitrate_bps;
121 double media_ratio; // Part of the total bitrate used for media [0.0, 1.0].
Lu Liu39260c42017-10-17 19:58:53 +0000122 std::string track_id;
mflodman2ebe5b12016-05-13 01:43:51 -0700123 };
124
perkj71ee44c2016-06-15 00:47:53 -0700125 // Calculates the minimum requested send bitrate and max padding bitrate and
126 // calls LimitObserver::OnAllocationLimitsChanged.
127 void UpdateAllocationLimits();
128
mflodman48a4beb2016-07-01 13:03:59 +0200129 typedef std::vector<ObserverConfig> ObserverConfigs;
130 ObserverConfigs::iterator FindObserverConfig(
perkj26091b12016-09-01 01:17:40 -0700131 const BitrateAllocatorObserver* observer);
mflodman2ebe5b12016-05-13 01:43:51 -0700132
133 typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap;
134 typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
135
perkj26091b12016-09-01 01:17:40 -0700136 ObserverAllocation AllocateBitrates(uint32_t bitrate);
Stefan Holmere5904162015-03-26 11:11:06 +0100137
perkj26091b12016-09-01 01:17:40 -0700138 ObserverAllocation ZeroRateAllocation();
139 ObserverAllocation LowRateAllocation(uint32_t bitrate);
mflodman101f2502016-06-09 17:21:19 +0200140 ObserverAllocation NormalRateAllocation(uint32_t bitrate,
perkj26091b12016-09-01 01:17:40 -0700141 uint32_t sum_min_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200142 ObserverAllocation MaxRateAllocation(uint32_t bitrate,
perkj26091b12016-09-01 01:17:40 -0700143 uint32_t sum_max_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200144
perkj26091b12016-09-01 01:17:40 -0700145 uint32_t LastAllocatedBitrate(const ObserverConfig& observer_config);
mflodman101f2502016-06-09 17:21:19 +0200146 // The minimum bitrate required by this observer, including enable-hysteresis
147 // if the observer is in a paused state.
perkj26091b12016-09-01 01:17:40 -0700148 uint32_t MinBitrateWithHysteresis(const ObserverConfig& observer_config);
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,
perkj26091b12016-09-01 01:17:40 -0700156 ObserverAllocation* allocation);
157 bool EnoughBitrateForAllObservers(uint32_t bitrate,
158 uint32_t sum_min_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200159
perkj26091b12016-09-01 01:17:40 -0700160 rtc::SequencedTaskChecker sequenced_checker_;
danilchapa37de392017-09-09 04:17:22 -0700161 LimitObserver* const limit_observer_ RTC_GUARDED_BY(&sequenced_checker_);
Stefan Holmere5904162015-03-26 11:11:06 +0100162 // Stored in a list to keep track of the insertion order.
danilchapa37de392017-09-09 04:17:22 -0700163 ObserverConfigs bitrate_observer_configs_ RTC_GUARDED_BY(&sequenced_checker_);
164 uint32_t last_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
165 uint32_t last_non_zero_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
166 uint8_t last_fraction_loss_ RTC_GUARDED_BY(&sequenced_checker_);
167 int64_t last_rtt_ RTC_GUARDED_BY(&sequenced_checker_);
168 int64_t last_bwe_period_ms_ RTC_GUARDED_BY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200169 // Number of mute events based on too low BWE, not network up/down.
danilchapa37de392017-09-09 04:17:22 -0700170 int num_pause_events_ RTC_GUARDED_BY(&sequenced_checker_);
171 Clock* const clock_ RTC_GUARDED_BY(&sequenced_checker_);
172 int64_t last_bwe_log_time_ RTC_GUARDED_BY(&sequenced_checker_);
173 uint32_t total_requested_padding_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
174 uint32_t total_requested_min_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000175};
176} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200177#endif // CALL_BITRATE_ALLOCATOR_H_