• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "absl/strings/string_view.h"
21 #include "api/network_state_predictor.h"
22 #include "api/sequence_checker.h"
23 #include "api/task_queue/task_queue_base.h"
24 #include "api/task_queue/task_queue_factory.h"
25 #include "api/transport/network_control.h"
26 #include "api/units/data_rate.h"
27 #include "call/rtp_bitrate_configurator.h"
28 #include "call/rtp_transport_config.h"
29 #include "call/rtp_transport_controller_send_interface.h"
30 #include "call/rtp_video_sender.h"
31 #include "modules/congestion_controller/rtp/control_handler.h"
32 #include "modules/congestion_controller/rtp/transport_feedback_adapter.h"
33 #include "modules/congestion_controller/rtp/transport_feedback_demuxer.h"
34 #include "modules/pacing/packet_router.h"
35 #include "modules/pacing/rtp_packet_pacer.h"
36 #include "modules/pacing/task_queue_paced_sender.h"
37 #include "modules/utility/maybe_worker_thread.h"
38 #include "rtc_base/network_route.h"
39 #include "rtc_base/race_checker.h"
40 #include "rtc_base/task_queue.h"
41 #include "rtc_base/task_utils/repeating_task.h"
42 
43 namespace webrtc {
44 class Clock;
45 class FrameEncryptorInterface;
46 class RtcEventLog;
47 
48 class RtpTransportControllerSend final
49     : public RtpTransportControllerSendInterface,
50       public RtcpBandwidthObserver,
51       public TransportFeedbackObserver,
52       public NetworkStateEstimateObserver {
53  public:
54   RtpTransportControllerSend(Clock* clock, const RtpTransportConfig& config);
55   ~RtpTransportControllerSend() override;
56 
57   RtpTransportControllerSend(const RtpTransportControllerSend&) = delete;
58   RtpTransportControllerSend& operator=(const RtpTransportControllerSend&) =
59       delete;
60 
61   // TODO(tommi): Change to std::unique_ptr<>.
62   RtpVideoSenderInterface* CreateRtpVideoSender(
63       const std::map<uint32_t, RtpState>& suspended_ssrcs,
64       const std::map<uint32_t, RtpPayloadState>&
65           states,  // move states into RtpTransportControllerSend
66       const RtpConfig& rtp_config,
67       int rtcp_report_interval_ms,
68       Transport* send_transport,
69       const RtpSenderObservers& observers,
70       RtcEventLog* event_log,
71       std::unique_ptr<FecController> fec_controller,
72       const RtpSenderFrameEncryptionConfig& frame_encryption_config,
73       rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) override;
74   void DestroyRtpVideoSender(
75       RtpVideoSenderInterface* rtp_video_sender) override;
76 
77   // Implements RtpTransportControllerSendInterface
78   MaybeWorkerThread* GetWorkerQueue() override;
79   PacketRouter* packet_router() override;
80 
81   NetworkStateEstimateObserver* network_state_estimate_observer() override;
82   TransportFeedbackObserver* transport_feedback_observer() override;
83   RtpPacketSender* packet_sender() override;
84 
85   void SetAllocatedSendBitrateLimits(BitrateAllocationLimits limits) override;
86 
87   void SetPacingFactor(float pacing_factor) override;
88   void SetQueueTimeLimit(int limit_ms) override;
89   StreamFeedbackProvider* GetStreamFeedbackProvider() override;
90   void RegisterTargetTransferRateObserver(
91       TargetTransferRateObserver* observer) override;
92   void OnNetworkRouteChanged(absl::string_view transport_name,
93                              const rtc::NetworkRoute& network_route) override;
94   void OnNetworkAvailability(bool network_available) override;
95   RtcpBandwidthObserver* GetBandwidthObserver() override;
96   int64_t GetPacerQueuingDelayMs() const override;
97   absl::optional<Timestamp> GetFirstPacketTime() const override;
98   void EnablePeriodicAlrProbing(bool enable) override;
99   void OnSentPacket(const rtc::SentPacket& sent_packet) override;
100   void OnReceivedPacket(const ReceivedPacket& packet_msg) override;
101 
102   void SetSdpBitrateParameters(const BitrateConstraints& constraints) override;
103   void SetClientBitratePreferences(const BitrateSettings& preferences) override;
104 
105   void OnTransportOverheadChanged(
106       size_t transport_overhead_bytes_per_packet) override;
107 
108   void AccountForAudioPacketsInPacedSender(bool account_for_audio) override;
109   void IncludeOverheadInPacedSender() override;
110   void EnsureStarted() override;
111 
112   // Implements RtcpBandwidthObserver interface
113   void OnReceivedEstimatedBitrate(uint32_t bitrate) override;
114   void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
115                                     int64_t rtt,
116                                     int64_t now_ms) override;
117 
118   // Implements TransportFeedbackObserver interface
119   void OnAddPacket(const RtpPacketSendInfo& packet_info) override;
120   void OnTransportFeedback(const rtcp::TransportFeedback& feedback) override;
121 
122   // Implements NetworkStateEstimateObserver interface
123   void OnRemoteNetworkEstimate(NetworkStateEstimate estimate) override;
124 
125  private:
126   struct PacerSettings {
127     explicit PacerSettings(const FieldTrialsView& trials);
128 
129     FieldTrialParameter<TimeDelta> holdback_window;
130     FieldTrialParameter<int> holdback_packets;
131   };
132 
133   void MaybeCreateControllers() RTC_RUN_ON(task_queue_);
134   void UpdateInitialConstraints(TargetRateConstraints new_contraints)
135       RTC_RUN_ON(task_queue_);
136 
137   void StartProcessPeriodicTasks() RTC_RUN_ON(task_queue_);
138   void UpdateControllerWithTimeInterval() RTC_RUN_ON(task_queue_);
139 
140   absl::optional<BitrateConstraints> ApplyOrLiftRelayCap(bool is_relayed);
141   bool IsRelevantRouteChange(const rtc::NetworkRoute& old_route,
142                              const rtc::NetworkRoute& new_route) const;
143   void UpdateBitrateConstraints(const BitrateConstraints& updated);
144   void UpdateStreamsConfig() RTC_RUN_ON(task_queue_);
145   void OnReceivedRtcpReceiverReportBlocks(const ReportBlockList& report_blocks,
146                                           int64_t now_ms)
147       RTC_RUN_ON(task_queue_);
148   void PostUpdates(NetworkControlUpdate update) RTC_RUN_ON(task_queue_);
149   void UpdateControlState() RTC_RUN_ON(task_queue_);
150   void UpdateCongestedState() RTC_RUN_ON(task_queue_);
151 
152   Clock* const clock_;
153   RtcEventLog* const event_log_;
154   TaskQueueFactory* const task_queue_factory_;
155   SequenceChecker main_thread_;
156   PacketRouter packet_router_;
157   std::vector<std::unique_ptr<RtpVideoSenderInterface>> video_rtp_senders_
158       RTC_GUARDED_BY(&main_thread_);
159   RtpBitrateConfigurator bitrate_configurator_;
160   std::map<std::string, rtc::NetworkRoute> network_routes_;
161   bool pacer_started_;
162   const PacerSettings pacer_settings_;
163   TaskQueuePacedSender pacer_;
164 
165   TargetTransferRateObserver* observer_ RTC_GUARDED_BY(task_queue_);
166   TransportFeedbackDemuxer feedback_demuxer_;
167 
168   TransportFeedbackAdapter transport_feedback_adapter_
169       RTC_GUARDED_BY(task_queue_);
170 
171   NetworkControllerFactoryInterface* const controller_factory_override_
172       RTC_PT_GUARDED_BY(task_queue_);
173   const std::unique_ptr<NetworkControllerFactoryInterface>
174       controller_factory_fallback_ RTC_PT_GUARDED_BY(task_queue_);
175 
176   std::unique_ptr<CongestionControlHandler> control_handler_
177       RTC_GUARDED_BY(task_queue_) RTC_PT_GUARDED_BY(task_queue_);
178 
179   std::unique_ptr<NetworkControllerInterface> controller_
180       RTC_GUARDED_BY(task_queue_) RTC_PT_GUARDED_BY(task_queue_);
181 
182   TimeDelta process_interval_ RTC_GUARDED_BY(task_queue_);
183 
184   std::map<uint32_t, RTCPReportBlock> last_report_blocks_
185       RTC_GUARDED_BY(task_queue_);
186   Timestamp last_report_block_time_ RTC_GUARDED_BY(task_queue_);
187 
188   NetworkControllerConfig initial_config_ RTC_GUARDED_BY(task_queue_);
189   StreamsConfig streams_config_ RTC_GUARDED_BY(task_queue_);
190 
191   const bool reset_feedback_on_route_change_;
192   const bool add_pacing_to_cwin_;
193   FieldTrialParameter<DataRate> relay_bandwidth_cap_;
194 
195   size_t transport_overhead_bytes_per_packet_ RTC_GUARDED_BY(task_queue_);
196   bool network_available_ RTC_GUARDED_BY(task_queue_);
197   RepeatingTaskHandle pacer_queue_update_task_ RTC_GUARDED_BY(task_queue_);
198   RepeatingTaskHandle controller_task_ RTC_GUARDED_BY(task_queue_);
199 
200   DataSize congestion_window_size_ RTC_GUARDED_BY(task_queue_);
201   bool is_congested_ RTC_GUARDED_BY(task_queue_);
202 
203   // Protected by internal locks.
204   RateLimiter retransmission_rate_limiter_;
205 
206   ScopedTaskSafety safety_;
207   MaybeWorkerThread task_queue_;
208 
209   const FieldTrialsView& field_trials_;
210 };
211 
212 }  // namespace webrtc
213 
214 #endif  // CALL_RTP_TRANSPORT_CONTROLLER_SEND_H_
215