1 /*
2  *  Copyright (c) 2017 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_RECEIVE_SIDE_CONGESTION_CONTROLLER_H_
12 #define MODULES_CONGESTION_CONTROLLER_INCLUDE_RECEIVE_SIDE_CONGESTION_CONTROLLER_H_
13 
14 #include <memory>
15 #include <vector>
16 
17 #include "api/transport/network_control.h"
18 #include "api/units/data_rate.h"
19 #include "api/units/time_delta.h"
20 #include "modules/congestion_controller/remb_throttler.h"
21 #include "modules/pacing/packet_router.h"
22 #include "modules/remote_bitrate_estimator/remote_estimator_proxy.h"
23 #include "rtc_base/synchronization/mutex.h"
24 #include "rtc_base/thread_annotations.h"
25 
26 namespace webrtc {
27 class RemoteBitrateEstimator;
28 
29 // This class represents the congestion control state for receive
30 // streams. For send side bandwidth estimation, this is simply
31 // relaying for each received RTP packet back to the sender. While for
32 // receive side bandwidth estimation, we do the estimation locally and
33 // send our results back to the sender.
34 class ReceiveSideCongestionController : public CallStatsObserver {
35  public:
36   ReceiveSideCongestionController(
37       Clock* clock,
38       RemoteEstimatorProxy::TransportFeedbackSender feedback_sender,
39       RembThrottler::RembSender remb_sender,
40       NetworkStateEstimator* network_state_estimator);
41 
~ReceiveSideCongestionController()42   ~ReceiveSideCongestionController() override {}
43 
44   virtual void OnReceivedPacket(int64_t arrival_time_ms,
45                                 size_t payload_size,
46                                 const RTPHeader& header);
47 
48   void SetSendPeriodicFeedback(bool send_periodic_feedback);
49 
50   // Implements CallStatsObserver.
51   void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
52 
53   // This is send bitrate, used to control the rate of feedback messages.
54   void OnBitrateChanged(int bitrate_bps);
55 
56   // Ensures the remote party is notified of the receive bitrate no larger than
57   // `bitrate` using RTCP REMB.
58   void SetMaxDesiredReceiveBitrate(DataRate bitrate);
59 
60   void SetTransportOverhead(DataSize overhead_per_packet);
61 
62   // Returns latest receive side bandwidth estimation.
63   // Returns zero if receive side bandwidth estimation is unavailable.
64   DataRate LatestReceiveSideEstimate() const;
65 
66   // Removes stream from receive side bandwidth estimation.
67   // Noop if receive side bwe is not used or stream doesn't participate in it.
68   void RemoveStream(uint32_t ssrc);
69 
70   // Runs periodic tasks if it is time to run them, returns time until next
71   // call to `MaybeProcess` should be non idle.
72   TimeDelta MaybeProcess();
73 
74  private:
75   void PickEstimatorFromHeader(const RTPHeader& header)
76       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
77   void PickEstimator() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
78 
79   Clock& clock_;
80   RembThrottler remb_throttler_;
81   RemoteEstimatorProxy remote_estimator_proxy_;
82 
83   mutable Mutex mutex_;
84   std::unique_ptr<RemoteBitrateEstimator> rbe_ RTC_GUARDED_BY(mutex_);
85   bool using_absolute_send_time_ RTC_GUARDED_BY(mutex_);
86   uint32_t packets_since_absolute_send_time_ RTC_GUARDED_BY(mutex_);
87 };
88 
89 }  // namespace webrtc
90 
91 #endif  // MODULES_CONGESTION_CONTROLLER_INCLUDE_RECEIVE_SIDE_CONGESTION_CONTROLLER_H_
92