blob: 481d91b23cedc0b8ba09f80c6c201437d57a9754 [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 Narest78609d52017-10-20 10:37:47 +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
Sebastian Jansson6736df12018-11-21 19:18:39 +010022#include "api/call/bitrate_allocation.h"
Sebastian Jansson93b1ea22019-09-18 18:31:52 +020023#include "api/transport/network_types.h"
Sebastian Janssonb55015e2019-04-09 13:44:04 +020024#include "rtc_base/synchronization/sequence_checker.h"
Mirko Bonadei20e4c802020-11-23 11:07:42 +010025#include "rtc_base/system/no_unique_address.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000026
27namespace webrtc {
28
mflodman48a4beb2016-07-01 13:03:59 +020029class Clock;
30
mflodman86aabb22016-03-11 15:44:32 +010031// Used by all send streams with adaptive bitrate, to get the currently
32// allocated bitrate for the send stream. The current network properties are
33// given at the same time, to let the send stream decide about possible loss
34// protection.
35class BitrateAllocatorObserver {
36 public:
mflodman48a4beb2016-07-01 13:03:59 +020037 // Returns the amount of protection used by the BitrateAllocatorObserver
38 // implementation, as bitrate in bps.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020039 virtual uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) = 0;
minyue78b4d562016-11-30 04:47:39 -080040
perkj71ee44c2016-06-15 00:47:53 -070041 protected:
mflodman86aabb22016-03-11 15:44:32 +010042 virtual ~BitrateAllocatorObserver() {}
43};
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000044
Sebastian Jansson24ad7202018-04-19 08:25:12 +020045// Struct describing parameters for how a media stream should get bitrate
Sebastian Jansson4d461ba2019-09-17 20:53:26 +020046// allocated to it.
47
Sebastian Jansson24ad7202018-04-19 08:25:12 +020048struct MediaStreamAllocationConfig {
Sebastian Jansson4d461ba2019-09-17 20:53:26 +020049 // Minimum bitrate supported by track. 0 equals no min bitrate.
Sebastian Jansson24ad7202018-04-19 08:25:12 +020050 uint32_t min_bitrate_bps;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +020051 // Maximum bitrate supported by track. 0 equals no max bitrate.
Sebastian Jansson24ad7202018-04-19 08:25:12 +020052 uint32_t max_bitrate_bps;
53 uint32_t pad_up_bitrate_bps;
Sebastian Jansson464a5572019-02-12 13:32:32 +010054 int64_t priority_bitrate_bps;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +020055 // True means track may not be paused by allocating 0 bitrate will allocate at
56 // least |min_bitrate_bps| for this observer, even if the BWE is too low,
57 // false will allocate 0 to the observer if BWE doesn't allow
58 // |min_bitrate_bps|.
Sebastian Jansson24ad7202018-04-19 08:25:12 +020059 bool enforce_min_bitrate;
Sebastian Jansson4d461ba2019-09-17 20:53:26 +020060 // The amount of bitrate allocated to this observer relative to all other
61 // observers. If an observer has twice the bitrate_priority of other
62 // observers, it should be allocated twice the bitrate above its min.
Sebastian Jansson24ad7202018-04-19 08:25:12 +020063 double bitrate_priority;
Sebastian Jansson24ad7202018-04-19 08:25:12 +020064};
65
Sebastian Jansson83267802018-04-19 08:27:19 +020066// Interface used for mocking
67class BitrateAllocatorInterface {
68 public:
69 virtual void AddObserver(BitrateAllocatorObserver* observer,
70 MediaStreamAllocationConfig config) = 0;
71 virtual void RemoveObserver(BitrateAllocatorObserver* observer) = 0;
Sebastian Jansson44a262a2018-10-24 16:07:20 +020072 virtual int GetStartBitrate(BitrateAllocatorObserver* observer) const = 0;
Sebastian Jansson83267802018-04-19 08:27:19 +020073
74 protected:
75 virtual ~BitrateAllocatorInterface() = default;
76};
77
Sebastian Jansson538ca572019-09-24 19:47:26 +020078namespace bitrate_allocator_impl {
79struct AllocatableTrack {
80 AllocatableTrack(BitrateAllocatorObserver* observer,
81 MediaStreamAllocationConfig allocation_config)
82 : observer(observer),
83 config(allocation_config),
84 allocated_bitrate_bps(-1),
85 media_ratio(1.0) {}
86 BitrateAllocatorObserver* observer;
87 MediaStreamAllocationConfig config;
88 int64_t allocated_bitrate_bps;
89 double media_ratio; // Part of the total bitrate used for media [0.0, 1.0].
90
91 uint32_t LastAllocatedBitrate() const;
92 // The minimum bitrate required by this observer, including
93 // enable-hysteresis if the observer is in a paused state.
94 uint32_t MinBitrateWithHysteresis() const;
95};
96} // namespace bitrate_allocator_impl
97
mflodman86aabb22016-03-11 15:44:32 +010098// Usage: this class will register multiple RtcpBitrateObserver's one at each
99// RTCP module. It will aggregate the results and run one bandwidth estimation
100// and push the result to the encoders via BitrateAllocatorObserver(s).
Sebastian Jansson83267802018-04-19 08:27:19 +0200101class BitrateAllocator : public BitrateAllocatorInterface {
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000102 public:
perkj71ee44c2016-06-15 00:47:53 -0700103 // Used to get notified when send stream limits such as the minimum send
104 // bitrate and max padding bitrate is changed.
105 class LimitObserver {
106 public:
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200107 virtual void OnAllocationLimitsChanged(BitrateAllocationLimits limits) = 0;
perkj71ee44c2016-06-15 00:47:53 -0700108
109 protected:
Sebastian Jansson83267802018-04-19 08:27:19 +0200110 virtual ~LimitObserver() = default;
perkj71ee44c2016-06-15 00:47:53 -0700111 };
112
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200113 explicit BitrateAllocator(LimitObserver* limit_observer);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200114 ~BitrateAllocator() override;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000115
Sebastian Jansson2701bc92018-12-11 15:02:47 +0100116 void UpdateStartRate(uint32_t start_rate_bps);
117
mflodman86aabb22016-03-11 15:44:32 +0100118 // Allocate target_bitrate across the registered BitrateAllocatorObservers.
Sebastian Jansson40de3cc2019-09-19 14:54:43 +0200119 void OnNetworkEstimateChanged(TargetTransferRate msg);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000120
Sebastian Jansson29b204e2018-03-21 12:45:27 +0100121 // Set the configuration used by the bandwidth management.
Peter Boström8e4e8b02015-09-15 15:08:03 +0200122 // |observer| updates bitrates if already in use.
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200123 // |config| is the configuration to use for allocation.
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200124 // Note that |observer|->OnBitrateUpdated() will be called
125 // within the scope of this method with the current rtt, fraction_loss and
126 // available bitrate and that the bitrate in OnBitrateUpdated will be zero if
127 // the |observer| is currently not allowed to send data.
perkj57c21f92016-06-17 07:27:16 -0700128 void AddObserver(BitrateAllocatorObserver* observer,
Sebastian Jansson83267802018-04-19 08:27:19 +0200129 MediaStreamAllocationConfig config) override;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000130
mflodman101f2502016-06-09 17:21:19 +0200131 // Removes a previously added observer, but will not trigger a new bitrate
132 // allocation.
Sebastian Jansson83267802018-04-19 08:27:19 +0200133 void RemoveObserver(BitrateAllocatorObserver* observer) override;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000134
perkj57c21f92016-06-17 07:27:16 -0700135 // Returns initial bitrate allocated for |observer|. If |observer| is not in
136 // the list of added observers, a best guess is returned.
Sebastian Jansson44a262a2018-10-24 16:07:20 +0200137 int GetStartBitrate(BitrateAllocatorObserver* observer) const override;
perkj57c21f92016-06-17 07:27:16 -0700138
mflodman2ebe5b12016-05-13 01:43:51 -0700139 private:
Sebastian Jansson538ca572019-09-24 19:47:26 +0200140 using AllocatableTrack = bitrate_allocator_impl::AllocatableTrack;
mflodman2ebe5b12016-05-13 01:43:51 -0700141
perkj71ee44c2016-06-15 00:47:53 -0700142 // Calculates the minimum requested send bitrate and max padding bitrate and
143 // calls LimitObserver::OnAllocationLimitsChanged.
Niels Möllerd4043f62018-04-26 16:06:22 +0200144 void UpdateAllocationLimits() RTC_RUN_ON(&sequenced_checker_);
perkj71ee44c2016-06-15 00:47:53 -0700145
Ying Wanga646d302018-03-02 17:04:11 +0100146 // Allow packets to be transmitted in up to 2 times max video bitrate if the
147 // bandwidth estimate allows it.
148 // TODO(bugs.webrtc.org/8541): May be worth to refactor to keep this logic in
Jonas Olsson0182a032019-07-09 12:31:20 +0200149 // video send stream.
Niels Möller74e5f802018-04-25 14:03:46 +0200150 static uint8_t GetTransmissionMaxBitrateMultiplier();
Ying Wanga646d302018-03-02 17:04:11 +0100151
Mirko Bonadei20e4c802020-11-23 11:07:42 +0100152 RTC_NO_UNIQUE_ADDRESS SequenceChecker sequenced_checker_;
danilchapa37de392017-09-09 04:17:22 -0700153 LimitObserver* const limit_observer_ RTC_GUARDED_BY(&sequenced_checker_);
Stefan Holmere5904162015-03-26 11:11:06 +0100154 // Stored in a list to keep track of the insertion order.
Sebastian Jansson4d461ba2019-09-17 20:53:26 +0200155 std::vector<AllocatableTrack> allocatable_tracks_
156 RTC_GUARDED_BY(&sequenced_checker_);
Sebastian Jansson89c94b92018-11-20 17:16:36 +0100157 uint32_t last_target_bps_ RTC_GUARDED_BY(&sequenced_checker_);
Florent Castelli4e615d52019-08-22 16:09:06 +0200158 uint32_t last_stable_target_bps_ RTC_GUARDED_BY(&sequenced_checker_);
danilchapa37de392017-09-09 04:17:22 -0700159 uint32_t last_non_zero_bitrate_bps_ RTC_GUARDED_BY(&sequenced_checker_);
160 uint8_t last_fraction_loss_ RTC_GUARDED_BY(&sequenced_checker_);
161 int64_t last_rtt_ RTC_GUARDED_BY(&sequenced_checker_);
162 int64_t last_bwe_period_ms_ RTC_GUARDED_BY(&sequenced_checker_);
mflodman48a4beb2016-07-01 13:03:59 +0200163 // Number of mute events based on too low BWE, not network up/down.
danilchapa37de392017-09-09 04:17:22 -0700164 int num_pause_events_ RTC_GUARDED_BY(&sequenced_checker_);
danilchapa37de392017-09-09 04:17:22 -0700165 int64_t last_bwe_log_time_ RTC_GUARDED_BY(&sequenced_checker_);
Sebastian Jansson93b1ea22019-09-18 18:31:52 +0200166 BitrateAllocationLimits current_limits_ RTC_GUARDED_BY(&sequenced_checker_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000167};
Seth Hampsonfe73d6a2017-11-14 10:49:06 -0800168
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000169} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200170#endif // CALL_BITRATE_ALLOCATOR_H_