blob: 3d162026007750bfbe4afb2fd995dd694e2f5dda [file] [log] [blame]
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +00001/*
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.org2e10b8e2013-07-16 12:54:53 +000018#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000019
stefan@webrtc.org1281dc02012-08-13 16:13:09 +000020#include <list>
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000021#include <map>
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000022#include <utility>
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000023
pbos@webrtc.org2e10b8e2013-07-16 12:54:53 +000024#include "webrtc/modules/bitrate_controller/send_side_bandwidth_estimation.h"
25#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
henrik.lundin@webrtc.orgb56d0e32013-10-24 09:24:06 +000026#include "webrtc/system_wrappers/interface/scoped_ptr.h"
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000027
28namespace webrtc {
29
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000030class BitrateControllerImpl : public BitrateController {
31 public:
andresp@webrtc.org44caf012014-03-26 21:00:21 +000032 BitrateControllerImpl(Clock* clock, bool enforce_min_bitrate);
andresp@webrtc.org16b75c22014-03-21 14:00:51 +000033 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,
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000040 uint32_t start_bitrate,
41 uint32_t min_bitrate,
42 uint32_t max_bitrate) OVERRIDE;
andresp@webrtc.org16b75c22014-03-21 14:00:51 +000043
44 virtual void RemoveBitrateObserver(BitrateObserver* observer) OVERRIDE;
45
46 virtual void EnforceMinBitrate(bool enforce_min_bitrate) OVERRIDE;
solenberg@webrtc.org4e656022014-03-26 14:32:47 +000047 virtual void SetReservedBitrate(uint32_t reserved_bitrate_bps) OVERRIDE;
andresp@webrtc.org16b75c22014-03-21 14:00:51 +000048
pkasting@chromium.org0b1534c2014-12-15 22:09:40 +000049 virtual int64_t TimeUntilNextProcess() OVERRIDE;
andresp@webrtc.org44caf012014-03-26 21:00:21 +000050 virtual int32_t Process() OVERRIDE;
51
andresp@webrtc.org16b75c22014-03-21 14:00:51 +000052 private:
53 class RtcpBandwidthObserverImpl;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000054
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000055 struct BitrateConfiguration {
56 BitrateConfiguration(uint32_t start_bitrate,
57 uint32_t min_bitrate,
58 uint32_t max_bitrate)
59 : start_bitrate_(start_bitrate),
60 min_bitrate_(min_bitrate),
61 max_bitrate_(max_bitrate) {
62 }
63 uint32_t start_bitrate_;
64 uint32_t min_bitrate_;
65 uint32_t max_bitrate_;
66 };
67 struct ObserverConfiguration {
68 ObserverConfiguration(BitrateObserver* observer,
69 uint32_t bitrate)
70 : observer_(observer),
71 min_bitrate_(bitrate) {
72 }
73 BitrateObserver* observer_;
74 uint32_t min_bitrate_;
75 };
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000076 typedef std::pair<BitrateObserver*, BitrateConfiguration*>
77 BitrateObserverConfiguration;
78 typedef std::list<BitrateObserverConfiguration> BitrateObserverConfList;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000079
andresp@webrtc.org07bc7342014-03-21 16:51:01 +000080 void UpdateMinMaxBitrate() EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
henrik.lundin@webrtc.orgb56d0e32013-10-24 09:24:06 +000081
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000082 // Called by BitrateObserver's direct from the RTCP module.
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000083 void OnReceivedEstimatedBitrate(uint32_t bitrate);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000084
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000085 void OnReceivedRtcpReceiverReport(uint8_t fraction_loss,
86 uint32_t rtt,
87 int number_of_packets,
88 int64_t now_ms);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000089
andresp@webrtc.org07bc7342014-03-21 16:51:01 +000090 void MaybeTriggerOnNetworkChanged() EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
91
stefan@webrtc.orgedeea912014-12-08 19:46:23 +000092 void OnNetworkChanged(uint32_t bitrate,
93 uint8_t fraction_loss, // 0 - 255.
94 uint32_t rtt)
andresp@webrtc.org16b75c22014-03-21 14:00:51 +000095 EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
96
97 void NormalRateAllocation(uint32_t bitrate,
98 uint8_t fraction_loss,
99 uint32_t rtt,
100 uint32_t sum_min_bitrates)
101 EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
102
103 void LowRateAllocation(uint32_t bitrate,
104 uint8_t fraction_loss,
105 uint32_t rtt,
106 uint32_t sum_min_bitrates)
107 EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
108
109 typedef std::multimap<uint32_t, ObserverConfiguration*> ObserverSortingMap;
110
111 BitrateObserverConfList::iterator FindObserverConfigurationPair(
112 const BitrateObserver* observer) EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000113
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000114 // Used by process thread.
115 Clock* clock_;
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000116 int64_t last_bitrate_update_ms_;
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000117
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000118 CriticalSectionWrapper* critsect_;
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000119 SendSideBandwidthEstimation bandwidth_estimation_ GUARDED_BY(*critsect_);
120 BitrateObserverConfList bitrate_observers_ GUARDED_BY(*critsect_);
121 bool enforce_min_bitrate_ GUARDED_BY(*critsect_);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000122 uint32_t reserved_bitrate_bps_ GUARDED_BY(*critsect_);
123
124 uint32_t last_bitrate_bps_ GUARDED_BY(*critsect_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000125 uint8_t last_fraction_loss_ GUARDED_BY(*critsect_);
andresp@webrtc.org44caf012014-03-26 21:00:21 +0000126 uint32_t last_rtt_ms_ GUARDED_BY(*critsect_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000127 bool last_enforce_min_bitrate_ GUARDED_BY(*critsect_);
128 bool bitrate_observers_modified_ GUARDED_BY(*critsect_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000129 uint32_t last_reserved_bitrate_bps_ GUARDED_BY(*critsect_);
solenberg@webrtc.org4e656022014-03-26 14:32:47 +0000130
131 DISALLOW_IMPLICIT_CONSTRUCTORS(BitrateControllerImpl);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000132};
133} // namespace webrtc
134#endif // WEBRTC_MODULES_BITRATE_CONTROLLER_BITRATE_CONTROLLER_IMPL_H_