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 
18 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
19 
20 #include <list>
21 #include <utility>
22 
23 #include "webrtc/base/criticalsection.h"
24 #include "webrtc/base/scoped_ptr.h"
25 #include "webrtc/modules/bitrate_controller/send_side_bandwidth_estimation.h"
26 
27 namespace webrtc {
28 
29 class BitrateControllerImpl : public BitrateController {
30  public:
31   BitrateControllerImpl(Clock* clock, BitrateObserver* observer);
~BitrateControllerImpl()32   virtual ~BitrateControllerImpl() {}
33 
34   bool AvailableBandwidth(uint32_t* bandwidth) const override;
35 
36   RtcpBandwidthObserver* CreateRtcpBandwidthObserver() override;
37 
38   void SetStartBitrate(int start_bitrate_bps) override;
39   void SetMinMaxBitrate(int min_bitrate_bps,
40                                 int max_bitrate_bps) override;
41 
42   void SetReservedBitrate(uint32_t reserved_bitrate_bps) override;
43 
44   void SetEventLog(RtcEventLog* event_log) override;
45 
46   int64_t TimeUntilNextProcess() override;
47   int32_t Process() override;
48 
49  private:
50   class RtcpBandwidthObserverImpl;
51 
52   // Called by BitrateObserver's direct from the RTCP module.
53   void OnReceivedEstimatedBitrate(uint32_t bitrate);
54 
55   void OnReceivedRtcpReceiverReport(uint8_t fraction_loss,
56                                     int64_t rtt,
57                                     int number_of_packets,
58                                     int64_t now_ms);
59 
60   void MaybeTriggerOnNetworkChanged();
61 
62   // Returns true if the parameters have changed since the last call.
63   bool GetNetworkParameters(uint32_t* bitrate,
64                             uint8_t* fraction_loss,
65                             int64_t* rtt);
66 
67   void OnNetworkChanged(uint32_t bitrate,
68                         uint8_t fraction_loss,  // 0 - 255.
69                         int64_t rtt) EXCLUSIVE_LOCKS_REQUIRED(critsect_);
70 
71   // Used by process thread.
72   Clock* clock_;
73   BitrateObserver* observer_;
74   int64_t last_bitrate_update_ms_;
75 
76   mutable rtc::CriticalSection critsect_;
77   SendSideBandwidthEstimation bandwidth_estimation_ GUARDED_BY(critsect_);
78   uint32_t reserved_bitrate_bps_ GUARDED_BY(critsect_);
79 
80   uint32_t last_bitrate_bps_ GUARDED_BY(critsect_);
81   uint8_t last_fraction_loss_ GUARDED_BY(critsect_);
82   int64_t last_rtt_ms_ GUARDED_BY(critsect_);
83   uint32_t last_reserved_bitrate_bps_ GUARDED_BY(critsect_);
84 
85   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(BitrateControllerImpl);
86 };
87 }  // namespace webrtc
88 #endif  // WEBRTC_MODULES_BITRATE_CONTROLLER_BITRATE_CONTROLLER_IMPL_H_
89