xref: /aosp_15_r20/external/webrtc/modules/congestion_controller/remb_throttler.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 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 #ifndef MODULES_CONGESTION_CONTROLLER_REMB_THROTTLER_H_
11 #define MODULES_CONGESTION_CONTROLLER_REMB_THROTTLER_H_
12 
13 #include <functional>
14 #include <vector>
15 
16 #include "api/units/data_rate.h"
17 #include "api/units/time_delta.h"
18 #include "api/units/timestamp.h"
19 #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
20 #include "rtc_base/synchronization/mutex.h"
21 
22 namespace webrtc {
23 
24 // RembThrottler is a helper class used for throttling RTCP REMB messages.
25 // Throttles small changes to the received BWE within 200ms.
26 class RembThrottler : public RemoteBitrateObserver {
27  public:
28   using RembSender =
29       std::function<void(int64_t bitrate_bps, std::vector<uint32_t> ssrcs)>;
30   RembThrottler(RembSender remb_sender, Clock* clock);
31 
32   // Ensures the remote party is notified of the receive bitrate no larger than
33   // `bitrate` using RTCP REMB.
34   void SetMaxDesiredReceiveBitrate(DataRate bitrate);
35 
36   // Implements RemoteBitrateObserver;
37   // Called every time there is a new bitrate estimate for a receive channel
38   // group. This call will trigger a new RTCP REMB packet if the bitrate
39   // estimate has decreased or if no RTCP REMB packet has been sent for
40   // a certain time interval.
41   void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
42                                uint32_t bitrate_bps) override;
43 
44  private:
45   const RembSender remb_sender_;
46   Clock* const clock_;
47   mutable Mutex mutex_;
48   Timestamp last_remb_time_ RTC_GUARDED_BY(mutex_);
49   DataRate last_send_remb_bitrate_ RTC_GUARDED_BY(mutex_);
50   DataRate max_remb_bitrate_ RTC_GUARDED_BY(mutex_);
51 };
52 
53 }  // namespace webrtc
54 #endif  // MODULES_CONGESTION_CONTROLLER_REMB_THROTTLER_H_
55