blob: f9d2354ca18da845410f1eb6124506003c473f69 [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.org16b75c22014-03-21 14:00:51 +000032 explicit BitrateControllerImpl(bool enforce_min_bitrate);
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;
47
andresp@webrtc.org16b75c22014-03-21 14:00:51 +000048 private:
49 class RtcpBandwidthObserverImpl;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000050
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000051 struct BitrateConfiguration {
52 BitrateConfiguration(uint32_t start_bitrate,
53 uint32_t min_bitrate,
54 uint32_t max_bitrate)
55 : start_bitrate_(start_bitrate),
56 min_bitrate_(min_bitrate),
57 max_bitrate_(max_bitrate) {
58 }
59 uint32_t start_bitrate_;
60 uint32_t min_bitrate_;
61 uint32_t max_bitrate_;
62 };
63 struct ObserverConfiguration {
64 ObserverConfiguration(BitrateObserver* observer,
65 uint32_t bitrate)
66 : observer_(observer),
67 min_bitrate_(bitrate) {
68 }
69 BitrateObserver* observer_;
70 uint32_t min_bitrate_;
71 };
henrik.lundin@webrtc.org29dd0de2013-10-21 14:00:01 +000072 typedef std::pair<BitrateObserver*, BitrateConfiguration*>
73 BitrateObserverConfiguration;
74 typedef std::list<BitrateObserverConfiguration> BitrateObserverConfList;
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000075
andresp@webrtc.org07bc7342014-03-21 16:51:01 +000076 void UpdateMinMaxBitrate() EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
henrik.lundin@webrtc.orgb56d0e32013-10-24 09:24:06 +000077
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000078 // Called by BitrateObserver's direct from the RTCP module.
79 void OnReceivedEstimatedBitrate(const uint32_t bitrate);
80
81 void OnReceivedRtcpReceiverReport(const uint8_t fraction_loss,
82 const uint32_t rtt,
83 const int number_of_packets,
84 const uint32_t now_ms);
85
andresp@webrtc.org07bc7342014-03-21 16:51:01 +000086 void MaybeTriggerOnNetworkChanged() EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
87
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +000088 void OnNetworkChanged(const uint32_t bitrate,
89 const uint8_t fraction_loss, // 0 - 255.
andresp@webrtc.org16b75c22014-03-21 14:00:51 +000090 const uint32_t rtt)
91 EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
92
93 void NormalRateAllocation(uint32_t bitrate,
94 uint8_t fraction_loss,
95 uint32_t rtt,
96 uint32_t sum_min_bitrates)
97 EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
98
99 void LowRateAllocation(uint32_t bitrate,
100 uint8_t fraction_loss,
101 uint32_t rtt,
102 uint32_t sum_min_bitrates)
103 EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
104
105 typedef std::multimap<uint32_t, ObserverConfiguration*> ObserverSortingMap;
106
107 BitrateObserverConfList::iterator FindObserverConfigurationPair(
108 const BitrateObserver* observer) EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000109
110 CriticalSectionWrapper* critsect_;
andresp@webrtc.org16b75c22014-03-21 14:00:51 +0000111 SendSideBandwidthEstimation bandwidth_estimation_ GUARDED_BY(*critsect_);
112 BitrateObserverConfList bitrate_observers_ GUARDED_BY(*critsect_);
113 bool enforce_min_bitrate_ GUARDED_BY(*critsect_);
andresp@webrtc.org07bc7342014-03-21 16:51:01 +0000114 uint32_t last_bitrate_ GUARDED_BY(*critsect_);
115 uint8_t last_fraction_loss_ GUARDED_BY(*critsect_);
116 uint32_t last_rtt_ GUARDED_BY(*critsect_);
117 bool last_enforce_min_bitrate_ GUARDED_BY(*critsect_);
118 bool bitrate_observers_modified_ GUARDED_BY(*critsect_);
pwestin@webrtc.org1cd11622012-04-19 12:13:52 +0000119};
120} // namespace webrtc
121#endif // WEBRTC_MODULES_BITRATE_CONTROLLER_BITRATE_CONTROLLER_IMPL_H_