blob: 25ca735585adc6b6b6f06cb8417a7d641aaa9e0a [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
14#include <list>
15#include <map>
16#include <utility>
17
tommi63cb4342016-01-20 02:32:54 -080018#include "webrtc/base/criticalsection.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000019#include "webrtc/base/scoped_ptr.h"
20#include "webrtc/base/thread_annotations.h"
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000021
22namespace webrtc {
23
mflodman86aabb22016-03-11 15:44:32 +010024// Used by all send streams with adaptive bitrate, to get the currently
25// allocated bitrate for the send stream. The current network properties are
26// given at the same time, to let the send stream decide about possible loss
27// protection.
28class BitrateAllocatorObserver {
29 public:
30 virtual void OnBitrateUpdated(uint32_t bitrate_bps,
31 uint8_t fraction_loss,
32 int64_t rtt) = 0;
33 virtual ~BitrateAllocatorObserver() {}
34};
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000035
mflodman86aabb22016-03-11 15:44:32 +010036// Usage: this class will register multiple RtcpBitrateObserver's one at each
37// RTCP module. It will aggregate the results and run one bandwidth estimation
38// and push the result to the encoders via BitrateAllocatorObserver(s).
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000039class BitrateAllocator {
40 public:
41 BitrateAllocator();
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000042
mflodman86aabb22016-03-11 15:44:32 +010043 // Allocate target_bitrate across the registered BitrateAllocatorObservers.
sprang2f48d942015-11-05 04:25:49 -080044 // Returns actual bitrate allocated (might be higher than target_bitrate if
45 // for instance EnforceMinBitrate() is enabled.
46 uint32_t OnNetworkChanged(uint32_t target_bitrate,
47 uint8_t fraction_loss,
48 int64_t rtt);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000049
50 // Set the start and max send bitrate used by the bandwidth management.
51 //
Peter Boström8e4e8b02015-09-15 15:08:03 +020052 // |observer| updates bitrates if already in use.
53 // |min_bitrate_bps| = 0 equals no min bitrate.
54 // |max_bitrate_bps| = 0 equals no max bitrate.
55 // Returns bitrate allocated for the bitrate observer.
mflodman86aabb22016-03-11 15:44:32 +010056 int AddObserver(BitrateAllocatorObserver* observer,
57 uint32_t min_bitrate_bps,
58 uint32_t max_bitrate_bps);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000059
mflodman86aabb22016-03-11 15:44:32 +010060 void RemoveObserver(BitrateAllocatorObserver* observer);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000061
62 void GetMinMaxBitrateSumBps(int* min_bitrate_sum_bps,
63 int* max_bitrate_sum_bps) const;
64
65 // This method controls the behavior when the available bitrate is lower than
66 // the minimum bitrate, or the sum of minimum bitrates.
67 // When true, the bitrate will never be set lower than the minimum bitrate(s).
68 // When false, the bitrate observers will be allocated rates up to their
69 // respective minimum bitrate, satisfying one observer after the other.
70 void EnforceMinBitrate(bool enforce_min_bitrate);
71
72 private:
73 struct BitrateConfiguration {
Peter Boström8e4e8b02015-09-15 15:08:03 +020074 BitrateConfiguration(uint32_t min_bitrate, uint32_t max_bitrate)
75 : min_bitrate(min_bitrate), max_bitrate(max_bitrate) {}
76 uint32_t min_bitrate;
77 uint32_t max_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000078 };
79 struct ObserverConfiguration {
mflodman86aabb22016-03-11 15:44:32 +010080 ObserverConfiguration(BitrateAllocatorObserver* observer, uint32_t bitrate)
Peter Boström8e4e8b02015-09-15 15:08:03 +020081 : observer(observer), min_bitrate(bitrate) {}
mflodman86aabb22016-03-11 15:44:32 +010082 BitrateAllocatorObserver* const observer;
Peter Boström8e4e8b02015-09-15 15:08:03 +020083 uint32_t min_bitrate;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000084 };
mflodman86aabb22016-03-11 15:44:32 +010085 typedef std::pair<BitrateAllocatorObserver*, BitrateConfiguration>
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000086 BitrateObserverConfiguration;
87 typedef std::list<BitrateObserverConfiguration> BitrateObserverConfList;
Stefan Holmere5904162015-03-26 11:11:06 +010088 typedef std::multimap<uint32_t, ObserverConfiguration> ObserverSortingMap;
mflodman86aabb22016-03-11 15:44:32 +010089 typedef std::map<BitrateAllocatorObserver*, int> ObserverBitrateMap;
stefan@webrtc.org792f1a12015-03-04 12:24:26 +000090
91 BitrateObserverConfList::iterator FindObserverConfigurationPair(
mflodman86aabb22016-03-11 15:44:32 +010092 const BitrateAllocatorObserver* observer)
93 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
Stefan Holmere5904162015-03-26 11:11:06 +010094 ObserverBitrateMap AllocateBitrates() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
95 ObserverBitrateMap NormalRateAllocation(uint32_t bitrate,
96 uint32_t sum_min_bitrates)
97 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
98
99 ObserverBitrateMap LowRateAllocation(uint32_t bitrate)
100 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000101
pbos5ad935c2016-01-25 03:52:44 -0800102 rtc::CriticalSection crit_sect_;
Stefan Holmere5904162015-03-26 11:11:06 +0100103 // Stored in a list to keep track of the insertion order.
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000104 BitrateObserverConfList bitrate_observers_ GUARDED_BY(crit_sect_);
105 bool bitrate_observers_modified_ GUARDED_BY(crit_sect_);
106 bool enforce_min_bitrate_ GUARDED_BY(crit_sect_);
Stefan Holmere5904162015-03-26 11:11:06 +0100107 uint32_t last_bitrate_bps_ GUARDED_BY(crit_sect_);
108 uint8_t last_fraction_loss_ GUARDED_BY(crit_sect_);
109 int64_t last_rtt_ GUARDED_BY(crit_sect_);
stefan@webrtc.org792f1a12015-03-04 12:24:26 +0000110};
111} // namespace webrtc
mflodman0e7e2592015-11-12 21:02:42 -0800112#endif // WEBRTC_CALL_BITRATE_ALLOCATOR_H_