blob: cf518f6c3df71880218072ee73ede4ee3303dd7f [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 Naresta5fbc232017-10-18 18:31:07 +020017#include <memory>
Alex Narestb3944f02017-10-13 14:56:18 +020018#include <string>
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000019#include <utility>
mflodman48a4beb2016-07-01 13:03:59 +020020#include <vector>
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000021
Alex Naresta5fbc232017-10-18 18:31:07 +020022#include "rtc_base/bitrateallocationstrategy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/sequenced_task_checker.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000024
25namespace webrtc {
26
mflodman48a4beb2016-07-01 13:03:59 +020027class Clock;
28
mflodman86aabb22016-03-11 15:44:32 +010029// Used by all send streams with adaptive bitrate, to get the currently
30// allocated bitrate for the send stream. The current network properties are
31// given at the same time, to let the send stream decide about possible loss
32// protection.
33class BitrateAllocatorObserver {
34 public:
mflodman48a4beb2016-07-01 13:03:59 +020035 // Returns the amount of protection used by the BitrateAllocatorObserver
36 // implementation, as bitrate in bps.
37 virtual uint32_t OnBitrateUpdated(uint32_t bitrate_bps,
38 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080039 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070040 int64_t bwe_period_ms) = 0;
minyue78b4d562016-11-30 04:47:39 -080041
perkj71ee44c2016-06-15 00:47:53 -070042 protected:
mflodman86aabb22016-03-11 15:44:32 +010043 virtual ~BitrateAllocatorObserver() {}
44};
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000045
mflodman86aabb22016-03-11 15:44:32 +010046// Usage: this class will register multiple RtcpBitrateObserver's one at each
47// RTCP module. It will aggregate the results and run one bandwidth estimation
48// and push the result to the encoders via BitrateAllocatorObserver(s).
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000049class BitrateAllocator {
50 public:
perkj71ee44c2016-06-15 00:47:53 -070051 // Used to get notified when send stream limits such as the minimum send
52 // bitrate and max padding bitrate is changed.
53 class LimitObserver {
54 public:
55 virtual void OnAllocationLimitsChanged(
56 uint32_t min_send_bitrate_bps,
57 uint32_t max_padding_bitrate_bps) = 0;
58
59 protected:
60 virtual ~LimitObserver() {}
61 };
62
63 explicit BitrateAllocator(LimitObserver* limit_observer);
mflodman48a4beb2016-07-01 13:03:59 +020064 ~BitrateAllocator();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000065
mflodman86aabb22016-03-11 15:44:32 +010066 // Allocate target_bitrate across the registered BitrateAllocatorObservers.
perkj71ee44c2016-06-15 00:47:53 -070067 void OnNetworkChanged(uint32_t target_bitrate_bps,
68 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -080069 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -070070 int64_t bwe_period_ms);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000071
72 // Set the start and max send bitrate used by the bandwidth management.
73 //
Peter Boström8e4e8b02015-09-15 15:08:03 +020074 // |observer| updates bitrates if already in use.
75 // |min_bitrate_bps| = 0 equals no min bitrate.
76 // |max_bitrate_bps| = 0 equals no max bitrate.
mflodman2ebe5b12016-05-13 01:43:51 -070077 // |enforce_min_bitrate| = 'true' will allocate at least |min_bitrate_bps| for
78 // this observer, even if the BWE is too low, 'false' will allocate 0 to
79 // the observer if BWE doesn't allow |min_bitrate_bps|.
perkjfea93092016-05-14 00:58:48 -070080 // Note that |observer|->OnBitrateUpdated() will be called within the scope of
81 // this method with the current rtt, fraction_loss and available bitrate and
82 // that the bitrate in OnBitrateUpdated will be zero if the |observer| is
83 // currently not allowed to send data.
perkj57c21f92016-06-17 07:27:16 -070084 void AddObserver(BitrateAllocatorObserver* observer,
85 uint32_t min_bitrate_bps,
86 uint32_t max_bitrate_bps,
87 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +020088 bool enforce_min_bitrate,
89 std::string track_id);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000090
mflodman101f2502016-06-09 17:21:19 +020091 // Removes a previously added observer, but will not trigger a new bitrate
92 // allocation.
mflodman86aabb22016-03-11 15:44:32 +010093 void RemoveObserver(BitrateAllocatorObserver* observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000094
perkj57c21f92016-06-17 07:27:16 -070095 // Returns initial bitrate allocated for |observer|. If |observer| is not in
96 // the list of added observers, a best guess is returned.
97 int GetStartBitrate(BitrateAllocatorObserver* observer);
98
Alex Naresta5fbc232017-10-18 18:31:07 +020099 // Sets external allocation strategy. If strategy is not set default WebRTC
100 // allocation mechanism will be used. The strategy may be changed during call.
101 // Setting NULL value will restore default WEBRTC allocation strategy.
102 void SetBitrateAllocationStrategy(
103 std::unique_ptr<rtc::BitrateAllocationStrategy>
104 bitrate_allocation_strategy);
105
mflodman2ebe5b12016-05-13 01:43:51 -0700106 private:
mflodman101f2502016-06-09 17:21:19 +0200107 // Note: All bitrates for member variables and methods are in bps.
Alex Naresta5fbc232017-10-18 18:31:07 +0200108 struct ObserverConfig : rtc::BitrateAllocationStrategy::TrackConfig {
mflodman2ebe5b12016-05-13 01:43:51 -0700109 ObserverConfig(BitrateAllocatorObserver* observer,
110 uint32_t min_bitrate_bps,
111 uint32_t max_bitrate_bps,
perkj71ee44c2016-06-15 00:47:53 -0700112 uint32_t pad_up_bitrate_bps,
Alex Narestb3944f02017-10-13 14:56:18 +0200113 bool enforce_min_bitrate,
114 std::string track_id)
Alex Naresta5fbc232017-10-18 18:31:07 +0200115 : TrackConfig(min_bitrate_bps,
116 max_bitrate_bps,
117 enforce_min_bitrate,
118 track_id),
119 observer(observer),
perkj71ee44c2016-06-15 00:47:53 -0700120 pad_up_bitrate_bps(pad_up_bitrate_bps),
mflodman48a4beb2016-07-01 13:03:59 +0200121 allocated_bitrate_bps(-1),
Alex Naresta5fbc232017-10-18 18:31:07 +0200122 media_ratio(1.0) {}
mflodman48a4beb2016-07-01 13:03:59 +0200123
124 BitrateAllocatorObserver* observer;
perkj71ee44c2016-06-15 00:47:53 -0700125 uint32_t pad_up_bitrate_bps;
mflodman48a4beb2016-07-01 13:03:59 +0200126 int64_t allocated_bitrate_bps;
127 double media_ratio; // Part of the total bitrate used for media [0.0, 1.0].
mflodman2ebe5b12016-05-13 01:43:51 -0700128 };
129
perkj71ee44c2016-06-15 00:47:53 -0700130 // Calculates the minimum requested send bitrate and max padding bitrate and
131 // calls LimitObserver::OnAllocationLimitsChanged.
132 void UpdateAllocationLimits();
133
mflodman48a4beb2016-07-01 13:03:59 +0200134 typedef std::vector<ObserverConfig> ObserverConfigs;
135 ObserverConfigs::iterator FindObserverConfig(
perkj26091b12016-09-01 01:17:40 -0700136 const BitrateAllocatorObserver* observer);
mflodman2ebe5b12016-05-13 01:43:51 -0700137
138 typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap;
139 typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
140
perkj26091b12016-09-01 01:17:40 -0700141 ObserverAllocation AllocateBitrates(uint32_t bitrate);
Stefan Holmere5904162015-03-26 11:11:06 +0100142
perkj26091b12016-09-01 01:17:40 -0700143 ObserverAllocation ZeroRateAllocation();
144 ObserverAllocation LowRateAllocation(uint32_t bitrate);
mflodman101f2502016-06-09 17:21:19 +0200145 ObserverAllocation NormalRateAllocation(uint32_t bitrate,
perkj26091b12016-09-01 01:17:40 -0700146 uint32_t sum_min_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200147 ObserverAllocation MaxRateAllocation(uint32_t bitrate,
perkj26091b12016-09-01 01:17:40 -0700148 uint32_t sum_max_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200149
perkj26091b12016-09-01 01:17:40 -0700150 uint32_t LastAllocatedBitrate(const ObserverConfig& observer_config);
mflodman101f2502016-06-09 17:21:19 +0200151 // The minimum bitrate required by this observer, including enable-hysteresis
152 // if the observer is in a paused state.
perkj26091b12016-09-01 01:17:40 -0700153 uint32_t MinBitrateWithHysteresis(const ObserverConfig& observer_config);
mflodman101f2502016-06-09 17:21:19 +0200154 // Splits |bitrate| evenly to observers already in |allocation|.
155 // |include_zero_allocations| decides if zero allocations should be part of
156 // the distribution or not. The allowed max bitrate is |max_multiplier| x
157 // observer max bitrate.
158 void DistributeBitrateEvenly(uint32_t bitrate,
159 bool include_zero_allocations,
160 int max_multiplier,
perkj26091b12016-09-01 01:17:40 -0700161 ObserverAllocation* allocation);
162 bool EnoughBitrateForAllObservers(uint32_t bitrate,
163 uint32_t sum_min_bitrates);
mflodman101f2502016-06-09 17:21:19 +0200164
perkj26091b12016-09-01 01:17:40 -0700165 rtc::SequencedTaskChecker sequenced_checker_;
danilchapa37de392017-09-09 04:17:22 -0700166 LimitObserver* const limit_observer_ RTC_GUARDED_BY(&sequenced_checker_);
Stefan Holmere5904162015-03-26 11:11:06 +0100167 // Stored in a list to keep track of the insertion order.
danilchapa37de392017-09-09 04:17:22 -0700168 ObserverConfigs bitrate_observer_configs_ RTC_GUARDED_BY(&sequenced_checker_);
169 uint32_t last_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
170 uint32_t last_non_zero_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
171 uint8_t last_fraction_loss_ RTC_GUARDED_BY(&sequenced_checker_);
172 int64_t last_rtt_ RTC_GUARDED_BY(&sequenced_checker_);
173 int64_t last_bwe_period_ms_ RTC_GUARDED_BY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200174 // Number of mute events based on too low BWE, not network up/down.
danilchapa37de392017-09-09 04:17:22 -0700175 int num_pause_events_ RTC_GUARDED_BY(&sequenced_checker_);
176 Clock* const clock_ RTC_GUARDED_BY(&sequenced_checker_);
177 int64_t last_bwe_log_time_ RTC_GUARDED_BY(&sequenced_checker_);
178 uint32_t total_requested_padding_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
179 uint32_t total_requested_min_bitrate_ RTC_GUARDED_BY(&sequenced_checker_);
Alex Naresta5fbc232017-10-18 18:31:07 +0200180 std::unique_ptr<rtc::BitrateAllocationStrategy> bitrate_allocation_strategy_
181 RTC_GUARDED_BY(&sequenced_checker_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000182};
183} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200184#endif // CALL_BITRATE_ALLOCATOR_H_