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 | |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 | [diff] [blame^] | 24 | #include "webrtc/base/scoped_ptr.h" |
sprang@webrtc.org | 9b79197 | 2014-12-18 11:53:59 | [diff] [blame] | 25 | #include "webrtc/modules/bitrate_controller/remb_suppressor.h" |
pbos@webrtc.org | 2e10b8e | 2013-07-16 12:54:53 | [diff] [blame] | 26 | #include "webrtc/modules/bitrate_controller/send_side_bandwidth_estimation.h" |
| 27 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 31 | class BitrateControllerImpl : public BitrateController { |
| 32 | public: |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 | [diff] [blame] | 33 | BitrateControllerImpl(Clock* clock, bool enforce_min_bitrate); |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 | [diff] [blame] | 34 | virtual ~BitrateControllerImpl(); |
| 35 | |
| 36 | virtual bool AvailableBandwidth(uint32_t* bandwidth) const OVERRIDE; |
| 37 | |
| 38 | virtual RtcpBandwidthObserver* CreateRtcpBandwidthObserver() OVERRIDE; |
| 39 | |
| 40 | virtual void SetBitrateObserver(BitrateObserver* observer, |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 | [diff] [blame] | 41 | uint32_t start_bitrate, |
| 42 | uint32_t min_bitrate, |
| 43 | uint32_t max_bitrate) OVERRIDE; |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 | [diff] [blame] | 44 | |
| 45 | virtual void RemoveBitrateObserver(BitrateObserver* observer) OVERRIDE; |
| 46 | |
| 47 | virtual void EnforceMinBitrate(bool enforce_min_bitrate) OVERRIDE; |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 | [diff] [blame] | 48 | virtual void SetReservedBitrate(uint32_t reserved_bitrate_bps) OVERRIDE; |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 | [diff] [blame] | 49 | |
pkasting@chromium.org | 0b1534c | 2014-12-15 22:09:40 | [diff] [blame] | 50 | virtual int64_t TimeUntilNextProcess() OVERRIDE; |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 | [diff] [blame] | 51 | virtual int32_t Process() OVERRIDE; |
| 52 | |
sprang@webrtc.org | 9b79197 | 2014-12-18 11:53:59 | [diff] [blame] | 53 | // Current bitrate actually being sent. |
| 54 | virtual void SetBitrateSent(uint32_t bitrate_sent_bps) OVERRIDE; |
| 55 | |
| 56 | virtual void SetCodecMode(webrtc::VideoCodecMode mode) OVERRIDE; |
| 57 | |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 | [diff] [blame] | 58 | private: |
| 59 | class RtcpBandwidthObserverImpl; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 60 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 61 | struct BitrateConfiguration { |
| 62 | BitrateConfiguration(uint32_t start_bitrate, |
| 63 | uint32_t min_bitrate, |
| 64 | uint32_t max_bitrate) |
| 65 | : start_bitrate_(start_bitrate), |
| 66 | min_bitrate_(min_bitrate), |
| 67 | max_bitrate_(max_bitrate) { |
| 68 | } |
| 69 | uint32_t start_bitrate_; |
| 70 | uint32_t min_bitrate_; |
| 71 | uint32_t max_bitrate_; |
| 72 | }; |
| 73 | struct ObserverConfiguration { |
| 74 | ObserverConfiguration(BitrateObserver* observer, |
| 75 | uint32_t bitrate) |
| 76 | : observer_(observer), |
| 77 | min_bitrate_(bitrate) { |
| 78 | } |
| 79 | BitrateObserver* observer_; |
| 80 | uint32_t min_bitrate_; |
| 81 | }; |
henrik.lundin@webrtc.org | 29dd0de | 2013-10-21 14:00:01 | [diff] [blame] | 82 | typedef std::pair<BitrateObserver*, BitrateConfiguration*> |
| 83 | BitrateObserverConfiguration; |
| 84 | typedef std::list<BitrateObserverConfiguration> BitrateObserverConfList; |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 85 | |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 | [diff] [blame] | 86 | void UpdateMinMaxBitrate() EXCLUSIVE_LOCKS_REQUIRED(*critsect_); |
henrik.lundin@webrtc.org | b56d0e3 | 2013-10-24 09:24:06 | [diff] [blame] | 87 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 88 | // Called by BitrateObserver's direct from the RTCP module. |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 | [diff] [blame] | 89 | void OnReceivedEstimatedBitrate(uint32_t bitrate); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 90 | |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 | [diff] [blame] | 91 | void OnReceivedRtcpReceiverReport(uint8_t fraction_loss, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 | [diff] [blame] | 92 | int64_t rtt, |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 | [diff] [blame] | 93 | int number_of_packets, |
| 94 | int64_t now_ms); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 95 | |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 | [diff] [blame] | 96 | void MaybeTriggerOnNetworkChanged() EXCLUSIVE_LOCKS_REQUIRED(*critsect_); |
| 97 | |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 | [diff] [blame] | 98 | void OnNetworkChanged(uint32_t bitrate, |
| 99 | uint8_t fraction_loss, // 0 - 255. |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 | [diff] [blame] | 100 | int64_t rtt) |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 | [diff] [blame] | 101 | EXCLUSIVE_LOCKS_REQUIRED(*critsect_); |
| 102 | |
| 103 | void NormalRateAllocation(uint32_t bitrate, |
| 104 | uint8_t fraction_loss, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 | [diff] [blame] | 105 | int64_t rtt, |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 | [diff] [blame] | 106 | uint32_t sum_min_bitrates) |
| 107 | EXCLUSIVE_LOCKS_REQUIRED(*critsect_); |
| 108 | |
| 109 | void LowRateAllocation(uint32_t bitrate, |
| 110 | uint8_t fraction_loss, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 | [diff] [blame] | 111 | int64_t rtt, |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 | [diff] [blame] | 112 | uint32_t sum_min_bitrates) |
| 113 | EXCLUSIVE_LOCKS_REQUIRED(*critsect_); |
| 114 | |
| 115 | typedef std::multimap<uint32_t, ObserverConfiguration*> ObserverSortingMap; |
| 116 | |
| 117 | BitrateObserverConfList::iterator FindObserverConfigurationPair( |
| 118 | const BitrateObserver* observer) EXCLUSIVE_LOCKS_REQUIRED(*critsect_); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 119 | |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 | [diff] [blame] | 120 | // Used by process thread. |
| 121 | Clock* clock_; |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 | [diff] [blame] | 122 | int64_t last_bitrate_update_ms_; |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 | [diff] [blame] | 123 | |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 124 | CriticalSectionWrapper* critsect_; |
andresp@webrtc.org | 16b75c2 | 2014-03-21 14:00:51 | [diff] [blame] | 125 | SendSideBandwidthEstimation bandwidth_estimation_ GUARDED_BY(*critsect_); |
| 126 | BitrateObserverConfList bitrate_observers_ GUARDED_BY(*critsect_); |
| 127 | bool enforce_min_bitrate_ GUARDED_BY(*critsect_); |
andresp@webrtc.org | 44caf01 | 2014-03-26 21:00:21 | [diff] [blame] | 128 | uint32_t reserved_bitrate_bps_ GUARDED_BY(*critsect_); |
| 129 | |
| 130 | uint32_t last_bitrate_bps_ GUARDED_BY(*critsect_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 | [diff] [blame] | 131 | uint8_t last_fraction_loss_ GUARDED_BY(*critsect_); |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 | [diff] [blame] | 132 | int64_t last_rtt_ms_ GUARDED_BY(*critsect_); |
andresp@webrtc.org | 07bc734 | 2014-03-21 16:51:01 | [diff] [blame] | 133 | bool last_enforce_min_bitrate_ GUARDED_BY(*critsect_); |
| 134 | bool bitrate_observers_modified_ GUARDED_BY(*critsect_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 | [diff] [blame] | 135 | uint32_t last_reserved_bitrate_bps_ GUARDED_BY(*critsect_); |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 | [diff] [blame^] | 136 | rtc::scoped_ptr<RembSuppressor> remb_suppressor_ GUARDED_BY(*critsect_); |
solenberg@webrtc.org | 4e65602 | 2014-03-26 14:32:47 | [diff] [blame] | 137 | |
| 138 | DISALLOW_IMPLICIT_CONSTRUCTORS(BitrateControllerImpl); |
pwestin@webrtc.org | 1cd1162 | 2012-04-19 12:13:52 | [diff] [blame] | 139 | }; |
| 140 | } // namespace webrtc |
| 141 | #endif // WEBRTC_MODULES_BITRATE_CONTROLLER_BITRATE_CONTROLLER_IMPL_H_ |