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 "modules/remote_bitrate_estimator/remote_estimator_proxy.h"
18 #include "rtc_base/constructormagic.h"
19 #include "rtc_base/criticalsection.h"
20 
21 namespace webrtc {
22 class RemoteBitrateEstimator;
23 class RemoteBitrateObserver;
24 
25 // This class represents the congestion control state for receive
26 // streams. For send side bandwidth estimation, this is simply
27 // relaying for each received RTP packet back to the sender. While for
28 // receive side bandwidth estimation, we do the estimation locally and
29 // send our results back to the sender.
30 class ReceiveSideCongestionController : public CallStatsObserver,
31                                         public Module {
32  public:
33   ReceiveSideCongestionController(
34       const Clock* clock,
35       PacketRouter* packet_router);
36 
~ReceiveSideCongestionController()37   virtual ~ReceiveSideCongestionController() {}
38 
39   virtual void OnReceivedPacket(int64_t arrival_time_ms,
40                                 size_t payload_size,
41                                 const RTPHeader& header);
42 
43   // TODO(nisse): Delete these methods, design a more specific interface.
44   virtual RemoteBitrateEstimator* GetRemoteBitrateEstimator(bool send_side_bwe);
45   virtual const RemoteBitrateEstimator* GetRemoteBitrateEstimator(
46       bool send_side_bwe) const;
47 
48   // Implements CallStatsObserver.
49   void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
50 
51   // This is send bitrate, used to control the rate of feedback messages.
52   void OnBitrateChanged(int bitrate_bps);
53 
54   // Implements Module.
55   int64_t TimeUntilNextProcess() override;
56   void Process() override;
57 
58  private:
59   class WrappingBitrateEstimator : public RemoteBitrateEstimator {
60    public:
61     WrappingBitrateEstimator(RemoteBitrateObserver* observer,
62                              const Clock* clock);
63 
~WrappingBitrateEstimator()64     virtual ~WrappingBitrateEstimator() {}
65 
66     void IncomingPacket(int64_t arrival_time_ms,
67                         size_t payload_size,
68                         const RTPHeader& header) override;
69 
70     void Process() override;
71 
72     int64_t TimeUntilNextProcess() override;
73 
74     void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override;
75 
76     void RemoveStream(unsigned int ssrc) override;
77 
78     bool LatestEstimate(std::vector<unsigned int>* ssrcs,
79                         unsigned int* bitrate_bps) const override;
80 
81     void SetMinBitrate(int min_bitrate_bps) override;
82 
83    private:
84     void PickEstimatorFromHeader(const RTPHeader& header)
85         RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
86     void PickEstimator() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
87     RemoteBitrateObserver* observer_;
88     const Clock* const clock_;
89     rtc::CriticalSection crit_sect_;
90     std::unique_ptr<RemoteBitrateEstimator> rbe_;
91     bool using_absolute_send_time_;
92     uint32_t packets_since_absolute_send_time_;
93     int min_bitrate_bps_;
94 
95     RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator);
96   };
97 
98   WrappingBitrateEstimator remote_bitrate_estimator_;
99   RemoteEstimatorProxy remote_estimator_proxy_;
100 };
101 
102 }  // namespace webrtc
103 
104 #endif  // MODULES_CONGESTION_CONTROLLER_INCLUDE_RECEIVE_SIDE_CONGESTION_CONTROLLER_H_
105