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 CALL_RTP_TRANSPORT_CONTROLLER_SEND_H_ 12 #define CALL_RTP_TRANSPORT_CONTROLLER_SEND_H_ 13 14 #include <atomic> 15 #include <map> 16 #include <memory> 17 #include <string> 18 #include <vector> 19 20 #include "api/network_state_predictor.h" 21 #include "api/transport/network_control.h" 22 #include "api/units/data_rate.h" 23 #include "call/rtp_bitrate_configurator.h" 24 #include "call/rtp_transport_controller_send_interface.h" 25 #include "call/rtp_video_sender.h" 26 #include "modules/congestion_controller/rtp/control_handler.h" 27 #include "modules/congestion_controller/rtp/transport_feedback_adapter.h" 28 #include "modules/congestion_controller/rtp/transport_feedback_demuxer.h" 29 #include "modules/pacing/paced_sender.h" 30 #include "modules/pacing/packet_router.h" 31 #include "modules/pacing/rtp_packet_pacer.h" 32 #include "modules/pacing/task_queue_paced_sender.h" 33 #include "modules/utility/include/process_thread.h" 34 #include "rtc_base/constructor_magic.h" 35 #include "rtc_base/network_route.h" 36 #include "rtc_base/race_checker.h" 37 #include "rtc_base/task_queue.h" 38 #include "rtc_base/task_utils/repeating_task.h" 39 40 namespace webrtc { 41 class Clock; 42 class FrameEncryptorInterface; 43 class RtcEventLog; 44 45 // TODO(nisse): When we get the underlying transports here, we should 46 // have one object implementing RtpTransportControllerSendInterface 47 // per transport, sharing the same congestion controller. 48 class RtpTransportControllerSend final 49 : public RtpTransportControllerSendInterface, 50 public RtcpBandwidthObserver, 51 public TransportFeedbackObserver, 52 public NetworkStateEstimateObserver { 53 public: 54 RtpTransportControllerSend( 55 Clock* clock, 56 RtcEventLog* event_log, 57 NetworkStatePredictorFactoryInterface* predictor_factory, 58 NetworkControllerFactoryInterface* controller_factory, 59 const BitrateConstraints& bitrate_config, 60 std::unique_ptr<ProcessThread> process_thread, 61 TaskQueueFactory* task_queue_factory, 62 const WebRtcKeyValueConfig* trials); 63 ~RtpTransportControllerSend() override; 64 65 RtpVideoSenderInterface* CreateRtpVideoSender( 66 std::map<uint32_t, RtpState> suspended_ssrcs, 67 const std::map<uint32_t, RtpPayloadState>& 68 states, // move states into RtpTransportControllerSend 69 const RtpConfig& rtp_config, 70 int rtcp_report_interval_ms, 71 Transport* send_transport, 72 const RtpSenderObservers& observers, 73 RtcEventLog* event_log, 74 std::unique_ptr<FecController> fec_controller, 75 const RtpSenderFrameEncryptionConfig& frame_encryption_config, 76 rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) override; 77 void DestroyRtpVideoSender( 78 RtpVideoSenderInterface* rtp_video_sender) override; 79 80 // Implements RtpTransportControllerSendInterface 81 rtc::TaskQueue* GetWorkerQueue() override; 82 PacketRouter* packet_router() override; 83 84 NetworkStateEstimateObserver* network_state_estimate_observer() override; 85 TransportFeedbackObserver* transport_feedback_observer() override; 86 RtpPacketSender* packet_sender() override; 87 88 void SetAllocatedSendBitrateLimits(BitrateAllocationLimits limits) override; 89 90 void SetPacingFactor(float pacing_factor) override; 91 void SetQueueTimeLimit(int limit_ms) override; 92 StreamFeedbackProvider* GetStreamFeedbackProvider() override; 93 void RegisterTargetTransferRateObserver( 94 TargetTransferRateObserver* observer) override; 95 void OnNetworkRouteChanged(const std::string& transport_name, 96 const rtc::NetworkRoute& network_route) override; 97 void OnNetworkAvailability(bool network_available) override; 98 RtcpBandwidthObserver* GetBandwidthObserver() override; 99 int64_t GetPacerQueuingDelayMs() const override; 100 absl::optional<Timestamp> GetFirstPacketTime() const override; 101 void EnablePeriodicAlrProbing(bool enable) override; 102 void OnSentPacket(const rtc::SentPacket& sent_packet) override; 103 void OnReceivedPacket(const ReceivedPacket& packet_msg) override; 104 105 void SetSdpBitrateParameters(const BitrateConstraints& constraints) override; 106 void SetClientBitratePreferences(const BitrateSettings& preferences) override; 107 108 void OnTransportOverheadChanged( 109 size_t transport_overhead_bytes_per_packet) override; 110 111 void AccountForAudioPacketsInPacedSender(bool account_for_audio) override; 112 void IncludeOverheadInPacedSender() override; 113 114 // Implements RtcpBandwidthObserver interface 115 void OnReceivedEstimatedBitrate(uint32_t bitrate) override; 116 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks, 117 int64_t rtt, 118 int64_t now_ms) override; 119 120 // Implements TransportFeedbackObserver interface 121 void OnAddPacket(const RtpPacketSendInfo& packet_info) override; 122 void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override; 123 124 // Implements NetworkStateEstimateObserver interface 125 void OnRemoteNetworkEstimate(NetworkStateEstimate estimate) override; 126 127 private: 128 void MaybeCreateControllers() RTC_RUN_ON(task_queue_); 129 void UpdateInitialConstraints(TargetRateConstraints new_contraints) 130 RTC_RUN_ON(task_queue_); 131 132 void StartProcessPeriodicTasks() RTC_RUN_ON(task_queue_); 133 void UpdateControllerWithTimeInterval() RTC_RUN_ON(task_queue_); 134 135 absl::optional<BitrateConstraints> ApplyOrLiftRelayCap(bool is_relayed); 136 bool IsRelevantRouteChange(const rtc::NetworkRoute& old_route, 137 const rtc::NetworkRoute& new_route) const; 138 void UpdateBitrateConstraints(const BitrateConstraints& updated); 139 void UpdateStreamsConfig() RTC_RUN_ON(task_queue_); 140 void OnReceivedRtcpReceiverReportBlocks(const ReportBlockList& report_blocks, 141 int64_t now_ms) 142 RTC_RUN_ON(task_queue_); 143 void PostUpdates(NetworkControlUpdate update) RTC_RUN_ON(task_queue_); 144 void UpdateControlState() RTC_RUN_ON(task_queue_); 145 RtpPacketPacer* pacer(); 146 const RtpPacketPacer* pacer() const; 147 148 Clock* const clock_; 149 RtcEventLog* const event_log_; 150 PacketRouter packet_router_; 151 std::vector<std::unique_ptr<RtpVideoSenderInterface>> video_rtp_senders_; 152 RtpBitrateConfigurator bitrate_configurator_; 153 std::map<std::string, rtc::NetworkRoute> network_routes_; 154 const std::unique_ptr<ProcessThread> process_thread_; 155 const bool use_task_queue_pacer_; 156 std::unique_ptr<PacedSender> process_thread_pacer_; 157 std::unique_ptr<TaskQueuePacedSender> task_queue_pacer_; 158 159 TargetTransferRateObserver* observer_ RTC_GUARDED_BY(task_queue_); 160 TransportFeedbackDemuxer feedback_demuxer_; 161 162 TransportFeedbackAdapter transport_feedback_adapter_ 163 RTC_GUARDED_BY(task_queue_); 164 165 NetworkControllerFactoryInterface* const controller_factory_override_ 166 RTC_PT_GUARDED_BY(task_queue_); 167 const std::unique_ptr<NetworkControllerFactoryInterface> 168 controller_factory_fallback_ RTC_PT_GUARDED_BY(task_queue_); 169 170 std::unique_ptr<CongestionControlHandler> control_handler_ 171 RTC_GUARDED_BY(task_queue_) RTC_PT_GUARDED_BY(task_queue_); 172 173 std::unique_ptr<NetworkControllerInterface> controller_ 174 RTC_GUARDED_BY(task_queue_) RTC_PT_GUARDED_BY(task_queue_); 175 176 TimeDelta process_interval_ RTC_GUARDED_BY(task_queue_); 177 178 std::map<uint32_t, RTCPReportBlock> last_report_blocks_ 179 RTC_GUARDED_BY(task_queue_); 180 Timestamp last_report_block_time_ RTC_GUARDED_BY(task_queue_); 181 182 NetworkControllerConfig initial_config_ RTC_GUARDED_BY(task_queue_); 183 StreamsConfig streams_config_ RTC_GUARDED_BY(task_queue_); 184 185 const bool reset_feedback_on_route_change_; 186 const bool send_side_bwe_with_overhead_; 187 const bool add_pacing_to_cwin_; 188 FieldTrialParameter<DataRate> relay_bandwidth_cap_; 189 190 size_t transport_overhead_bytes_per_packet_ RTC_GUARDED_BY(task_queue_); 191 bool network_available_ RTC_GUARDED_BY(task_queue_); 192 RepeatingTaskHandle pacer_queue_update_task_ RTC_GUARDED_BY(task_queue_); 193 RepeatingTaskHandle controller_task_ RTC_GUARDED_BY(task_queue_); 194 195 // Protected by internal locks. 196 RateLimiter retransmission_rate_limiter_; 197 198 // TODO(perkj): |task_queue_| is supposed to replace |process_thread_|. 199 // |task_queue_| is defined last to ensure all pending tasks are cancelled 200 // and deleted before any other members. 201 rtc::TaskQueue task_queue_; 202 RTC_DISALLOW_COPY_AND_ASSIGN(RtpTransportControllerSend); 203 }; 204 205 } // namespace webrtc 206 207 #endif // CALL_RTP_TRANSPORT_CONTROLLER_SEND_H_ 208