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 
11 #ifndef MODULES_CONGESTION_CONTROLLER_INCLUDE_SEND_SIDE_CONGESTION_CONTROLLER_H_
12 #define MODULES_CONGESTION_CONTROLLER_INCLUDE_SEND_SIDE_CONGESTION_CONTROLLER_H_
13 
14 #include <memory>
15 #include <vector>
16 
17 #include "common_types.h"  // NOLINT(build/include)
18 #include "modules/congestion_controller/delay_based_bwe.h"
19 #include "modules/congestion_controller/transport_feedback_adapter.h"
20 #include "modules/include/module.h"
21 #include "modules/include/module_common_types.h"
22 #include "modules/pacing/paced_sender.h"
23 #include "rtc_base/constructormagic.h"
24 #include "rtc_base/criticalsection.h"
25 #include "rtc_base/networkroute.h"
26 #include "rtc_base/race_checker.h"
27 
28 namespace rtc {
29 struct SentPacket;
30 }
31 
32 namespace webrtc {
33 
34 class BitrateController;
35 class Clock;
36 class AcknowledgedBitrateEstimator;
37 class ProbeController;
38 class RateLimiter;
39 class RtcEventLog;
40 
41 class SendSideCongestionController : public CallStatsObserver,
42                                      public Module,
43                                      public TransportFeedbackObserver {
44  public:
45   // Observer class for bitrate changes announced due to change in bandwidth
46   // estimate or due to that the send pacer is full. Fraction loss and rtt is
47   // also part of this callback to allow the observer to optimize its settings
48   // for different types of network environments. The bitrate does not include
49   // packet headers and is measured in bits per second.
50   class Observer {
51    public:
52     virtual void OnNetworkChanged(uint32_t bitrate_bps,
53                                   uint8_t fraction_loss,  // 0 - 255.
54                                   int64_t rtt_ms,
55                                   int64_t probing_interval_ms) = 0;
56 
57    protected:
~Observer()58     virtual ~Observer() {}
59   };
60   SendSideCongestionController(const Clock* clock,
61                                Observer* observer,
62                                RtcEventLog* event_log,
63                                PacedSender* pacer);
64   ~SendSideCongestionController() override;
65 
66   void RegisterPacketFeedbackObserver(PacketFeedbackObserver* observer);
67   void DeRegisterPacketFeedbackObserver(PacketFeedbackObserver* observer);
68 
69   // Currently, there can be at most one observer.
70   // TODO(nisse): The RegisterNetworkObserver method is needed because we first
71   // construct this object (as part of RtpTransportControllerSend), then pass a
72   // reference to Call, which then registers itself as the observer. We should
73   // try to break this circular chain of references, and make the observer a
74   // construction time constant.
75   void RegisterNetworkObserver(Observer* observer);
76   void DeRegisterNetworkObserver(Observer* observer);
77 
78   virtual void SetBweBitrates(int min_bitrate_bps,
79                               int start_bitrate_bps,
80                               int max_bitrate_bps);
81   // Resets the BWE state. Note the first argument is the bitrate_bps.
82   virtual void OnNetworkRouteChanged(const rtc::NetworkRoute& network_route,
83                                      int bitrate_bps,
84                                      int min_bitrate_bps,
85                                      int max_bitrate_bps);
86   virtual void SignalNetworkState(NetworkState state);
87   virtual void SetTransportOverhead(size_t transport_overhead_bytes_per_packet);
88 
89   // Deprecated: Use GetBandwidthObserver instead.
90   RTC_DEPRECATED virtual BitrateController* GetBitrateController() const;
91 
92   virtual RtcpBandwidthObserver* GetBandwidthObserver() const;
93 
94   virtual bool AvailableBandwidth(uint32_t* bandwidth) const;
95   virtual int64_t GetPacerQueuingDelayMs() const;
96   virtual int64_t GetFirstPacketTimeMs() const;
97 
98   virtual TransportFeedbackObserver* GetTransportFeedbackObserver();
99 
100   RateLimiter* GetRetransmissionRateLimiter();
101   void EnablePeriodicAlrProbing(bool enable);
102 
103   virtual void OnSentPacket(const rtc::SentPacket& sent_packet);
104 
105   // Implements CallStatsObserver.
106   void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
107 
108   // Implements Module.
109   int64_t TimeUntilNextProcess() override;
110   void Process() override;
111 
112   // Implements TransportFeedbackObserver.
113   void AddPacket(uint32_t ssrc,
114                  uint16_t sequence_number,
115                  size_t length,
116                  const PacedPacketInfo& pacing_info) override;
117   void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override;
118   std::vector<PacketFeedback> GetTransportFeedbackVector() const override;
119 
120  private:
121   void MaybeTriggerOnNetworkChanged();
122 
123   bool IsSendQueueFull() const;
124   bool IsNetworkDown() const;
125   bool HasNetworkParametersToReportChanged(uint32_t bitrate_bps,
126                                            uint8_t fraction_loss,
127                                            int64_t rtt);
128   void LimitOutstandingBytes(size_t num_outstanding_bytes);
129   const Clock* const clock_;
130   rtc::CriticalSection observer_lock_;
131   Observer* observer_ RTC_GUARDED_BY(observer_lock_);
132   RtcEventLog* const event_log_;
133   PacedSender* const pacer_;
134   const std::unique_ptr<BitrateController> bitrate_controller_;
135   std::unique_ptr<AcknowledgedBitrateEstimator> acknowledged_bitrate_estimator_;
136   const std::unique_ptr<ProbeController> probe_controller_;
137   const std::unique_ptr<RateLimiter> retransmission_rate_limiter_;
138   TransportFeedbackAdapter transport_feedback_adapter_;
139   rtc::CriticalSection network_state_lock_;
140   uint32_t last_reported_bitrate_bps_ RTC_GUARDED_BY(network_state_lock_);
141   uint8_t last_reported_fraction_loss_ RTC_GUARDED_BY(network_state_lock_);
142   int64_t last_reported_rtt_ RTC_GUARDED_BY(network_state_lock_);
143   NetworkState network_state_ RTC_GUARDED_BY(network_state_lock_);
144   bool pause_pacer_ RTC_GUARDED_BY(network_state_lock_);
145   // Duplicate the pacer paused state to avoid grabbing a lock when
146   // pausing the pacer. This can be removed when we move this class
147   // over to the task queue.
148   bool pacer_paused_;
149   rtc::CriticalSection bwe_lock_;
150   int min_bitrate_bps_ RTC_GUARDED_BY(bwe_lock_);
151   std::unique_ptr<DelayBasedBwe> delay_based_bwe_ RTC_GUARDED_BY(bwe_lock_);
152   bool in_cwnd_experiment_;
153   int64_t accepted_queue_ms_;
154   bool was_in_alr_;
155 
156   rtc::RaceChecker worker_race_;
157 
158   bool pacer_pushback_experiment_ = false;
159   float encoding_rate_ = 1.0;
160 
161   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(SendSideCongestionController);
162 };
163 
164 }  // namespace webrtc
165 
166 #endif  // MODULES_CONGESTION_CONTROLLER_INCLUDE_SEND_SIDE_CONGESTION_CONTROLLER_H_
167