receive_side_congestion_controller.h (3998B)
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 <cstdint> 15 #include <memory> 16 17 #include "api/environment/environment.h" 18 #include "api/media_types.h" 19 #include "api/sequence_checker.h" 20 #include "api/units/data_rate.h" 21 #include "api/units/time_delta.h" 22 #include "modules/congestion_controller/remb_throttler.h" 23 #include "modules/include/module_common_types.h" 24 #include "modules/remote_bitrate_estimator/congestion_control_feedback_generator.h" 25 #include "modules/remote_bitrate_estimator/transport_sequence_number_feedback_generator.h" 26 #include "modules/rtp_rtcp/source/rtp_packet_received.h" 27 #include "rtc_base/synchronization/mutex.h" 28 #include "rtc_base/thread_annotations.h" 29 30 namespace webrtc { 31 class RemoteBitrateEstimator; 32 33 // This class represents the congestion control state for receive 34 // streams. For send side bandwidth estimation, this is simply 35 // relaying for each received RTP packet back to the sender. While for 36 // receive side bandwidth estimation, we do the estimation locally and 37 // send our results back to the sender. 38 class ReceiveSideCongestionController : public CallStatsObserver { 39 public: 40 ReceiveSideCongestionController( 41 const Environment& env, 42 TransportSequenceNumberFeedbackGenenerator::RtcpSender feedback_sender, 43 RembThrottler::RembSender remb_sender); 44 45 ~ReceiveSideCongestionController() override = default; 46 47 void EnableSendCongestionControlFeedbackAccordingToRfc8888(); 48 49 void OnReceivedPacket(const RtpPacketReceived& packet, MediaType media_type); 50 51 // Implements CallStatsObserver. 52 void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) override; 53 54 // This is send bitrate, used to control the rate of feedback messages. 55 void OnBitrateChanged(int bitrate_bps); 56 57 // Ensures the remote party is notified of the receive bitrate no larger than 58 // `bitrate` using RTCP REMB. 59 void SetMaxDesiredReceiveBitrate(DataRate bitrate); 60 61 // Returns latest receive side bandwidth estimation. 62 // Returns zero if receive side bandwidth estimation is unavailable. 63 DataRate LatestReceiveSideEstimate() const; 64 65 // Removes stream from receive side bandwidth estimation. 66 // Noop if receive side bwe is not used or stream doesn't participate in it. 67 void RemoveStream(uint32_t ssrc); 68 69 // Runs periodic tasks if it is time to run them, returns time until next 70 // call to `MaybeProcess` should be non idle. 71 TimeDelta MaybeProcess(); 72 73 private: 74 void PickEstimator(bool has_absolute_send_time) 75 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_); 76 77 const Environment env_; 78 RembThrottler remb_throttler_; 79 80 // TODO: bugs.webrtc.org/42224904 - Use sequence checker for all usage of 81 // ReceiveSideCongestionController. At the time of 82 // writing OnReceivedPacket and MaybeProcess can unfortunately be called on an 83 // arbitrary thread by external projects. 84 SequenceChecker sequence_checker_; 85 86 bool send_rfc8888_congestion_feedback_ = false; 87 TransportSequenceNumberFeedbackGenenerator 88 transport_sequence_number_feedback_generator_; 89 CongestionControlFeedbackGenerator congestion_control_feedback_generator_ 90 RTC_GUARDED_BY(sequence_checker_); 91 92 mutable Mutex mutex_; 93 std::unique_ptr<RemoteBitrateEstimator> rbe_ RTC_GUARDED_BY(mutex_); 94 bool using_absolute_send_time_ RTC_GUARDED_BY(mutex_); 95 uint32_t packets_since_absolute_send_time_ RTC_GUARDED_BY(mutex_); 96 }; 97 98 } // namespace webrtc 99 100 #endif // MODULES_CONGESTION_CONTROLLER_INCLUDE_RECEIVE_SIDE_CONGESTION_CONTROLLER_H_