• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 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_PACING_PACED_SENDER_H_
12 #define MODULES_PACING_PACED_SENDER_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include <atomic>
18 #include <memory>
19 #include <vector>
20 
21 #include "absl/types/optional.h"
22 #include "api/function_view.h"
23 #include "api/transport/field_trial_based_config.h"
24 #include "api/transport/network_types.h"
25 #include "api/transport/webrtc_key_value_config.h"
26 #include "modules/include/module.h"
27 #include "modules/pacing/bitrate_prober.h"
28 #include "modules/pacing/interval_budget.h"
29 #include "modules/pacing/pacing_controller.h"
30 #include "modules/pacing/packet_router.h"
31 #include "modules/pacing/rtp_packet_pacer.h"
32 #include "modules/rtp_rtcp/include/rtp_packet_sender.h"
33 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
34 #include "modules/utility/include/process_thread.h"
35 #include "rtc_base/deprecated/recursive_critical_section.h"
36 #include "rtc_base/thread_annotations.h"
37 
38 namespace webrtc {
39 class Clock;
40 class RtcEventLog;
41 
42 // TODO(bugs.webrtc.org/10937): Remove the inheritance from Module after
43 // updating dependencies.
44 class PacedSender : public Module,
45                     public RtpPacketPacer,
46                     public RtpPacketSender {
47  public:
48   // Expected max pacer delay in ms. If ExpectedQueueTime() is higher than
49   // this value, the packet producers should wait (eg drop frames rather than
50   // encoding them). Bitrate sent may temporarily exceed target set by
51   // UpdateBitrate() so that this limit will be upheld.
52   static const int64_t kMaxQueueLengthMs;
53   // Pacing-rate relative to our target send rate.
54   // Multiplicative factor that is applied to the target bitrate to calculate
55   // the number of bytes that can be transmitted per interval.
56   // Increasing this factor will result in lower delays in cases of bitrate
57   // overshoots from the encoder.
58   static const float kDefaultPaceMultiplier;
59 
60   // TODO(bugs.webrtc.org/10937): Make the |process_thread| argument be non
61   // optional once all callers have been updated.
62   PacedSender(Clock* clock,
63               PacketRouter* packet_router,
64               RtcEventLog* event_log,
65               const WebRtcKeyValueConfig* field_trials = nullptr,
66               ProcessThread* process_thread = nullptr);
67 
68   ~PacedSender() override;
69 
70   // Methods implementing RtpPacketSender.
71 
72   // Adds the packet to the queue and calls PacketRouter::SendPacket() when
73   // it's time to send.
74   void EnqueuePackets(
75       std::vector<std::unique_ptr<RtpPacketToSend>> packet) override;
76 
77   // Methods implementing RtpPacketPacer:
78 
79   void CreateProbeCluster(DataRate bitrate, int cluster_id) override;
80 
81   // Temporarily pause all sending.
82   void Pause() override;
83 
84   // Resume sending packets.
85   void Resume() override;
86 
87   void SetCongestionWindow(DataSize congestion_window_size) override;
88   void UpdateOutstandingData(DataSize outstanding_data) override;
89 
90   // Sets the pacing rates. Must be called once before packets can be sent.
91   void SetPacingRates(DataRate pacing_rate, DataRate padding_rate) override;
92 
93   // Currently audio traffic is not accounted by pacer and passed through.
94   // With the introduction of audio BWE audio traffic will be accounted for
95   // the pacer budget calculation. The audio traffic still will be injected
96   // at high priority.
97   void SetAccountForAudioPackets(bool account_for_audio) override;
98 
99   void SetIncludeOverhead() override;
100   void SetTransportOverhead(DataSize overhead_per_packet) override;
101 
102   // Returns the time since the oldest queued packet was enqueued.
103   TimeDelta OldestPacketWaitTime() const override;
104 
105   DataSize QueueSizeData() const override;
106 
107   // Returns the time when the first packet was sent;
108   absl::optional<Timestamp> FirstSentPacketTime() const override;
109 
110   // Returns the number of milliseconds it will take to send the current
111   // packets in the queue, given the current size and bitrate, ignoring prio.
112   TimeDelta ExpectedQueueTime() const override;
113 
114   void SetQueueTimeLimit(TimeDelta limit) override;
115 
116   // Below are methods specific to this implementation, such as things related
117   // to module processing thread specifics or methods exposed for test.
118 
119  private:
120   // Methods implementing Module.
121   // TODO(bugs.webrtc.org/10937): Remove the inheritance from Module once all
122   // use of it has been cleared up.
123 
124   // Returns the number of milliseconds until the module want a worker thread
125   // to call Process.
126   int64_t TimeUntilNextProcess() override;
127 
128   // TODO(bugs.webrtc.org/10937): Make this private (and non virtual) once
129   // dependencies have been updated to not call this via the PacedSender
130   // interface.
131  public:
132   // Process any pending packets in the queue(s).
133   void Process() override;
134 
135  private:
136   // Called when the prober is associated with a process thread.
137   void ProcessThreadAttached(ProcessThread* process_thread) override;
138 
139   // In dynamic process mode, refreshes the next process time.
140   void MaybeWakupProcessThread();
141 
142   // Private implementation of Module to not expose those implementation details
143   // publicly and control when the class is registered/deregistered.
144   class ModuleProxy : public Module {
145    public:
ModuleProxy(PacedSender * delegate)146     explicit ModuleProxy(PacedSender* delegate) : delegate_(delegate) {}
147 
148    private:
TimeUntilNextProcess()149     int64_t TimeUntilNextProcess() override {
150       return delegate_->TimeUntilNextProcess();
151     }
Process()152     void Process() override { return delegate_->Process(); }
ProcessThreadAttached(ProcessThread * process_thread)153     void ProcessThreadAttached(ProcessThread* process_thread) override {
154       return delegate_->ProcessThreadAttached(process_thread);
155     }
156 
157     PacedSender* const delegate_;
158   } module_proxy_{this};
159 
160   rtc::RecursiveCriticalSection critsect_;
161   const PacingController::ProcessMode process_mode_;
162   PacingController pacing_controller_ RTC_GUARDED_BY(critsect_);
163 
164   Clock* const clock_;
165   ProcessThread* const process_thread_;
166 };
167 }  // namespace webrtc
168 #endif  // MODULES_PACING_PACED_SENDER_H_
169