pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 encoder via VideoEncoderCallback. |
| 13 | */ |
| 14 | |
| 15 | #ifndef WEBRTC_MODULES_BITRATE_CONTROLLER_BITRATE_CONTROLLER_IMPL_H_ |
| 16 | #define WEBRTC_MODULES_BITRATE_CONTROLLER_BITRATE_CONTROLLER_IMPL_H_ |
| 17 | |
pbos@webrtc.org | 2e10b8e | 2013-07-16 12:54:53 | [diff] [blame] | 18 | #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 19 | |
stefan@webrtc.org | 1281dc0 | 2012-08-13 16:13:09 | [diff] [blame] | 20 | #include <list> |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 21 | #include <map> |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 | [diff] [blame] | 22 | #include <utility> |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 23 | |
pbos@webrtc.org | 2e10b8e | 2013-07-16 12:54:53 | [diff] [blame] | 24 | #include "webrtc/modules/bitrate_controller/send_side_bandwidth_estimation.h" |
| 25 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 | [diff] [blame] | 26 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 30 | class BitrateControllerImpl : public BitrateController { |
| 31 | public: |
andrew@webrtc.org | 6cd201c | 2014-03-25 19:42:39 | [diff] [blame] | 32 | explicit BitrateControllerImpl(bool enforce_min_bitrate); |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 | [diff] [blame] | 33 | virtual ~BitrateControllerImpl(); |
| 34 | |
| 35 | virtual bool AvailableBandwidth(uint32_t* bandwidth) const OVERRIDE; |
| 36 | |
| 37 | virtual RtcpBandwidthObserver* CreateRtcpBandwidthObserver() OVERRIDE; |
| 38 | |
| 39 | virtual void SetBitrateObserver(BitrateObserver* observer, |
| 40 | const uint32_t start_bitrate, |
| 41 | const uint32_t min_bitrate, |
| 42 | const uint32_t max_bitrate) OVERRIDE; |
| 43 | |
| 44 | virtual void RemoveBitrateObserver(BitrateObserver* observer) OVERRIDE; |
| 45 | |
| 46 | virtual void EnforceMinBitrate(bool enforce_min_bitrate) OVERRIDE; |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 | [diff] [blame^] | 47 | virtual void SetReservedBitrate(uint32_t reserved_bitrate_bps) OVERRIDE; |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 | [diff] [blame] | 48 | |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 | [diff] [blame] | 49 | private: |
| 50 | class RtcpBandwidthObserverImpl; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 51 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 52 | struct BitrateConfiguration { |
| 53 | BitrateConfiguration(uint32_t start_bitrate, |
| 54 | uint32_t min_bitrate, |
| 55 | uint32_t max_bitrate) |
| 56 | : start_bitrate_(start_bitrate), |
| 57 | min_bitrate_(min_bitrate), |
| 58 | max_bitrate_(max_bitrate) { |
| 59 | } |
| 60 | uint32_t start_bitrate_; |
| 61 | uint32_t min_bitrate_; |
| 62 | uint32_t max_bitrate_; |
| 63 | }; |
| 64 | struct ObserverConfiguration { |
| 65 | ObserverConfiguration(BitrateObserver* observer, |
| 66 | uint32_t bitrate) |
| 67 | : observer_(observer), |
| 68 | min_bitrate_(bitrate) { |
| 69 | } |
| 70 | BitrateObserver* observer_; |
| 71 | uint32_t min_bitrate_; |
| 72 | }; |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 | [diff] [blame] | 73 | typedef std::pair<BitrateObserver*, BitrateConfiguration*> |
| 74 | BitrateObserverConfiguration; |
| 75 | typedef std::list<BitrateObserverConfiguration> BitrateObserverConfList; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 76 | |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 | [diff] [blame] | 77 | void UpdateMinMaxBitrate() EXCLUSIVE_LOCKS_REQUIRED(*critsect_); |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 | [diff] [blame] | 78 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 79 | // Called by BitrateObserver's direct from the RTCP module. |
| 80 | void OnReceivedEstimatedBitrate(const uint32_t bitrate); |
| 81 | |
| 82 | void OnReceivedRtcpReceiverReport(const uint8_t fraction_loss, |
| 83 | const uint32_t rtt, |
| 84 | const int number_of_packets, |
| 85 | const uint32_t now_ms); |
| 86 | |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 | [diff] [blame] | 87 | void MaybeTriggerOnNetworkChanged() EXCLUSIVE_LOCKS_REQUIRED(*critsect_); |
| 88 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 89 | void OnNetworkChanged(const uint32_t bitrate, |
| 90 | const uint8_t fraction_loss, // 0 - 255. |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 | [diff] [blame] | 91 | const uint32_t rtt) |
| 92 | EXCLUSIVE_LOCKS_REQUIRED(*critsect_); |
| 93 | |
| 94 | void NormalRateAllocation(uint32_t bitrate, |
| 95 | uint8_t fraction_loss, |
| 96 | uint32_t rtt, |
| 97 | uint32_t sum_min_bitrates) |
| 98 | EXCLUSIVE_LOCKS_REQUIRED(*critsect_); |
| 99 | |
| 100 | void LowRateAllocation(uint32_t bitrate, |
| 101 | uint8_t fraction_loss, |
| 102 | uint32_t rtt, |
| 103 | uint32_t sum_min_bitrates) |
| 104 | EXCLUSIVE_LOCKS_REQUIRED(*critsect_); |
| 105 | |
| 106 | typedef std::multimap<uint32_t, ObserverConfiguration*> ObserverSortingMap; |
| 107 | |
| 108 | BitrateObserverConfList::iterator FindObserverConfigurationPair( |
| 109 | const BitrateObserver* observer) EXCLUSIVE_LOCKS_REQUIRED(*critsect_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 110 | |
| 111 | CriticalSectionWrapper* critsect_; |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 | [diff] [blame] | 112 | SendSideBandwidthEstimation bandwidth_estimation_ GUARDED_BY(*critsect_); |
| 113 | BitrateObserverConfList bitrate_observers_ GUARDED_BY(*critsect_); |
| 114 | bool enforce_min_bitrate_ GUARDED_BY(*critsect_); |
andrew@webrtc.org | 6cd201c | 2014-03-25 19:42:39 | [diff] [blame] | 115 | uint32_t last_bitrate_ GUARDED_BY(*critsect_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 | [diff] [blame] | 116 | uint8_t last_fraction_loss_ GUARDED_BY(*critsect_); |
andrew@webrtc.org | 6cd201c | 2014-03-25 19:42:39 | [diff] [blame] | 117 | uint32_t last_rtt_ GUARDED_BY(*critsect_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 | [diff] [blame] | 118 | bool last_enforce_min_bitrate_ GUARDED_BY(*critsect_); |
| 119 | bool bitrate_observers_modified_ GUARDED_BY(*critsect_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 | [diff] [blame^] | 120 | uint32_t last_reserved_bitrate_bps_ GUARDED_BY(*critsect_); |
| 121 | uint32_t reserved_bitrate_bps_ GUARDED_BY(*critsect_); |
| 122 | |
| 123 | DISALLOW_IMPLICIT_CONSTRUCTORS(BitrateControllerImpl); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 124 | }; |
| 125 | } // namespace webrtc |
| 126 | #endif // WEBRTC_MODULES_BITRATE_CONTROLLER_BITRATE_CONTROLLER_IMPL_H_ |