stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
mflodman | 0e7e259 | 2015-11-12 21:02:42 -0800 | [diff] [blame] | 11 | #ifndef WEBRTC_CALL_BITRATE_ALLOCATOR_H_ |
| 12 | #define WEBRTC_CALL_BITRATE_ALLOCATOR_H_ |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 13 | |
kwiberg | b25345e | 2016-03-12 06:10:44 -0800 | [diff] [blame] | 14 | #include <stdint.h> |
| 15 | |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 16 | #include <list> |
| 17 | #include <map> |
| 18 | #include <utility> |
| 19 | |
tommi | 63cb434 | 2016-01-20 02:32:54 -0800 | [diff] [blame] | 20 | #include "webrtc/base/criticalsection.h" |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 21 | #include "webrtc/base/thread_annotations.h" |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 25 | // Used by all send streams with adaptive bitrate, to get the currently |
| 26 | // allocated bitrate for the send stream. The current network properties are |
| 27 | // given at the same time, to let the send stream decide about possible loss |
| 28 | // protection. |
| 29 | class BitrateAllocatorObserver { |
| 30 | public: |
| 31 | virtual void OnBitrateUpdated(uint32_t bitrate_bps, |
| 32 | uint8_t fraction_loss, |
| 33 | int64_t rtt) = 0; |
| 34 | virtual ~BitrateAllocatorObserver() {} |
| 35 | }; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 36 | |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 37 | // Usage: this class will register multiple RtcpBitrateObserver's one at each |
| 38 | // RTCP module. It will aggregate the results and run one bandwidth estimation |
| 39 | // and push the result to the encoders via BitrateAllocatorObserver(s). |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 40 | class BitrateAllocator { |
| 41 | public: |
| 42 | BitrateAllocator(); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 43 | |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 44 | // Allocate target_bitrate across the registered BitrateAllocatorObservers. |
sprang | 2f48d94 | 2015-11-05 04:25:49 -0800 | [diff] [blame] | 45 | // Returns actual bitrate allocated (might be higher than target_bitrate if |
| 46 | // for instance EnforceMinBitrate() is enabled. |
| 47 | uint32_t OnNetworkChanged(uint32_t target_bitrate, |
| 48 | uint8_t fraction_loss, |
| 49 | int64_t rtt); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 50 | |
| 51 | // Set the start and max send bitrate used by the bandwidth management. |
| 52 | // |
Peter Boström | 8e4e8b0 | 2015-09-15 15:08:03 +0200 | [diff] [blame] | 53 | // |observer| updates bitrates if already in use. |
| 54 | // |min_bitrate_bps| = 0 equals no min bitrate. |
| 55 | // |max_bitrate_bps| = 0 equals no max bitrate. |
| 56 | // Returns bitrate allocated for the bitrate observer. |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 57 | int AddObserver(BitrateAllocatorObserver* observer, |
| 58 | uint32_t min_bitrate_bps, |
| 59 | uint32_t max_bitrate_bps); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 60 | |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 61 | void RemoveObserver(BitrateAllocatorObserver* observer); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 62 | |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 63 | // This method controls the behavior when the available bitrate is lower than |
| 64 | // the minimum bitrate, or the sum of minimum bitrates. |
| 65 | // When true, the bitrate will never be set lower than the minimum bitrate(s). |
| 66 | // When false, the bitrate observers will be allocated rates up to their |
| 67 | // respective minimum bitrate, satisfying one observer after the other. |
| 68 | void EnforceMinBitrate(bool enforce_min_bitrate); |
| 69 | |
| 70 | private: |
| 71 | struct BitrateConfiguration { |
Peter Boström | 8e4e8b0 | 2015-09-15 15:08:03 +0200 | [diff] [blame] | 72 | BitrateConfiguration(uint32_t min_bitrate, uint32_t max_bitrate) |
| 73 | : min_bitrate(min_bitrate), max_bitrate(max_bitrate) {} |
| 74 | uint32_t min_bitrate; |
| 75 | uint32_t max_bitrate; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 76 | }; |
| 77 | struct ObserverConfiguration { |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 78 | ObserverConfiguration(BitrateAllocatorObserver* observer, uint32_t bitrate) |
Peter Boström | 8e4e8b0 | 2015-09-15 15:08:03 +0200 | [diff] [blame] | 79 | : observer(observer), min_bitrate(bitrate) {} |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 80 | BitrateAllocatorObserver* const observer; |
Peter Boström | 8e4e8b0 | 2015-09-15 15:08:03 +0200 | [diff] [blame] | 81 | uint32_t min_bitrate; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 82 | }; |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 83 | typedef std::pair<BitrateAllocatorObserver*, BitrateConfiguration> |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 84 | BitrateObserverConfiguration; |
| 85 | typedef std::list<BitrateObserverConfiguration> BitrateObserverConfList; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 86 | typedef std::multimap<uint32_t, ObserverConfiguration> ObserverSortingMap; |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 87 | typedef std::map<BitrateAllocatorObserver*, int> ObserverBitrateMap; |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 88 | |
| 89 | BitrateObserverConfList::iterator FindObserverConfigurationPair( |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 90 | const BitrateAllocatorObserver* observer) |
| 91 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 92 | ObserverBitrateMap AllocateBitrates() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 93 | ObserverBitrateMap NormalRateAllocation(uint32_t bitrate, |
| 94 | uint32_t sum_min_bitrates) |
| 95 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 96 | |
Per | 28a4456 | 2016-05-04 17:12:51 +0200 | [diff] [blame^] | 97 | ObserverBitrateMap ZeroRateAllocation() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 98 | ObserverBitrateMap LowRateAllocation(uint32_t bitrate) |
| 99 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 100 | |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 101 | rtc::CriticalSection crit_sect_; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 102 | // Stored in a list to keep track of the insertion order. |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 103 | BitrateObserverConfList bitrate_observers_ GUARDED_BY(crit_sect_); |
| 104 | bool bitrate_observers_modified_ GUARDED_BY(crit_sect_); |
| 105 | bool enforce_min_bitrate_ GUARDED_BY(crit_sect_); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 106 | uint32_t last_bitrate_bps_ GUARDED_BY(crit_sect_); |
| 107 | uint8_t last_fraction_loss_ GUARDED_BY(crit_sect_); |
| 108 | int64_t last_rtt_ GUARDED_BY(crit_sect_); |
stefan@webrtc.org | 792f1a1 | 2015-03-04 12:24:26 +0000 | [diff] [blame] | 109 | }; |
| 110 | } // namespace webrtc |
mflodman | 0e7e259 | 2015-11-12 21:02:42 -0800 | [diff] [blame] | 111 | #endif // WEBRTC_CALL_BITRATE_ALLOCATOR_H_ |