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