tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

task_queue_paced_sender.h (7371B)


      1 /*
      2 *  Copyright (c) 2019 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_TASK_QUEUE_PACED_SENDER_H_
     12 #define MODULES_PACING_TASK_QUEUE_PACED_SENDER_H_
     13 
     14 #include <stddef.h>
     15 #include <stdint.h>
     16 
     17 #include <memory>
     18 #include <optional>
     19 #include <vector>
     20 
     21 #include "api/field_trials_view.h"
     22 #include "api/rtp_packet_sender.h"
     23 #include "api/sequence_checker.h"
     24 #include "api/task_queue/pending_task_safety_flag.h"
     25 #include "api/task_queue/task_queue_base.h"
     26 #include "api/transport/network_types.h"
     27 #include "api/units/data_rate.h"
     28 #include "api/units/data_size.h"
     29 #include "api/units/time_delta.h"
     30 #include "api/units/timestamp.h"
     31 #include "modules/pacing/pacing_controller.h"
     32 #include "modules/pacing/rtp_packet_pacer.h"
     33 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
     34 #include "rtc_base/numerics/exp_filter.h"
     35 #include "rtc_base/thread_annotations.h"
     36 
     37 namespace webrtc {
     38 class Clock;
     39 
     40 class TaskQueuePacedSender : public RtpPacketPacer, public RtpPacketSender {
     41 public:
     42  static const int kNoPacketHoldback;
     43 
     44  // The pacer can be configured using `field_trials` or specified parameters.
     45  //
     46  // The `hold_back_window` parameter sets a lower bound on time to sleep if
     47  // there is currently a pacer queue and packets can't immediately be
     48  // processed. Increasing this reduces thread wakeups at the expense of higher
     49  // latency.
     50  //
     51  // The taskqueue used when constructing a TaskQueuePacedSender will also be
     52  // used for pacing.
     53  TaskQueuePacedSender(Clock* clock,
     54                       PacingController::PacketSender* packet_sender,
     55                       const FieldTrialsView& field_trials,
     56                       TimeDelta max_hold_back_window,
     57                       int max_hold_back_window_in_packets);
     58 
     59  ~TaskQueuePacedSender() override;
     60 
     61  // The pacer is allowed to send enqued packets in bursts and can build up a
     62  // packet "debt" that correspond to approximately the send rate during
     63  // 'burst_interval'.
     64  void SetSendBurstInterval(TimeDelta burst_interval);
     65 
     66  // A probe may be sent without first waing for a media packet.
     67  void SetAllowProbeWithoutMediaPacket(bool allow);
     68 
     69  // Ensure that necessary delayed tasks are scheduled.
     70  void EnsureStarted();
     71 
     72  // Methods implementing RtpPacketSender.
     73 
     74  // Adds the packet to the queue and calls
     75  // PacingController::PacketSender::SendPacket() when it's time to send.
     76  void EnqueuePackets(
     77      std::vector<std::unique_ptr<RtpPacketToSend>> packets) override;
     78  // Remove any pending packets matching this SSRC from the packet queue.
     79  void RemovePacketsForSsrc(uint32_t ssrc) override;
     80 
     81  // Methods implementing RtpPacketPacer.
     82 
     83  void CreateProbeClusters(
     84      std::vector<ProbeClusterConfig> probe_cluster_configs) override;
     85 
     86  // Temporarily pause all sending.
     87  void Pause() override;
     88 
     89  // Resume sending packets.
     90  void Resume() override;
     91 
     92  void SetCongested(bool congested) override;
     93 
     94  // Sets the pacing rates. Must be called once before packets can be sent.
     95  void SetPacingRates(DataRate pacing_rate, DataRate padding_rate) override;
     96 
     97  // Currently audio traffic is not accounted for by pacer and passed through.
     98  // With the introduction of audio BWE, audio traffic will be accounted for
     99  // in the pacer budget calculation. The audio traffic will still be injected
    100  // at high priority.
    101  void SetAccountForAudioPackets(bool account_for_audio) override;
    102 
    103  void SetIncludeOverhead() override;
    104  void SetTransportOverhead(DataSize overhead_per_packet) override;
    105 
    106  // Returns the time since the oldest queued packet was enqueued.
    107  TimeDelta OldestPacketWaitTime() const override;
    108 
    109  // Returns total size of all packets in the pacer queue.
    110  DataSize QueueSizeData() const override;
    111 
    112  // Returns the time when the first packet was sent;
    113  std::optional<Timestamp> FirstSentPacketTime() const override;
    114 
    115  // Returns the number of milliseconds it will take to send the current
    116  // packets in the queue, given the current size and bitrate, ignoring prio.
    117  TimeDelta ExpectedQueueTime() const override;
    118 
    119  // Set the max desired queuing delay, pacer will override the pacing rate
    120  // specified by SetPacingRates() if needed to achieve this goal.
    121  void SetQueueTimeLimit(TimeDelta limit) override;
    122 
    123 protected:
    124  // Exposed as protected for test.
    125  struct Stats {
    126    Stats()
    127        : oldest_packet_enqueue_time(Timestamp::MinusInfinity()),
    128          queue_size(DataSize::Zero()),
    129          expected_queue_time(TimeDelta::Zero()) {}
    130    Timestamp oldest_packet_enqueue_time;
    131    DataSize queue_size;
    132    TimeDelta expected_queue_time;
    133    std::optional<Timestamp> first_sent_packet_time;
    134  };
    135  void OnStatsUpdated(const Stats& stats);
    136 
    137 private:
    138  // Call in response to state updates that could warrant sending out packets.
    139  // API methods should use this method if the method can be executed as a
    140  // consequence of sending a packet to avoid sending another packet in the same
    141  // call stack.
    142  void PostMaybeProcessPackets() RTC_RUN_ON(task_queue_);
    143  // Check if it is time to send packets, or schedule a delayed task if not.
    144  // Use Timestamp::MinusInfinity() to indicate that this call has _not_
    145  // been scheduled by the pacing controller. If this is the case, check if we
    146  // can execute immediately otherwise schedule a delay task that calls this
    147  // method again with desired (finite) scheduled process time.
    148  void MaybeProcessPackets(Timestamp scheduled_process_time);
    149 
    150  void UpdateStats() RTC_RUN_ON(task_queue_);
    151  Stats GetStats() const;
    152 
    153  Clock* const clock_;
    154 
    155  // The holdback window prevents too frequent delayed MaybeProcessPackets()
    156  // calls. These are only applicable if `allow_low_precision` is false.
    157  const TimeDelta max_hold_back_window_;
    158  const int max_hold_back_window_in_packets_;
    159 
    160  PacingController pacing_controller_ RTC_GUARDED_BY(task_queue_);
    161 
    162  // We want only one (valid) delayed process task in flight at a time.
    163  // If the value of `next_process_time_` is finite, it is an id for a
    164  // delayed task that will call MaybeProcessPackets() with that time
    165  // as parameter.
    166  // Timestamp::MinusInfinity() indicates no valid pending task.
    167  Timestamp next_process_time_ RTC_GUARDED_BY(task_queue_);
    168 
    169  // Indicates if this task queue is started. If not, don't allow
    170  // posting delayed tasks yet.
    171  bool is_started_ RTC_GUARDED_BY(task_queue_);
    172 
    173  // Indicates if this task queue is shutting down. If so, don't allow
    174  // posting any more delayed tasks as that can cause the task queue to
    175  // never drain.
    176  bool is_shutdown_ RTC_GUARDED_BY(task_queue_);
    177 
    178  // Filtered size of enqueued packets, in bytes.
    179  ExpFilter packet_size_ RTC_GUARDED_BY(task_queue_);
    180  bool include_overhead_ RTC_GUARDED_BY(task_queue_);
    181 
    182  Stats current_stats_ RTC_GUARDED_BY(task_queue_);
    183  // Protects against ProcessPackets reentry from packet sent receipts.
    184  bool processing_packets_ RTC_GUARDED_BY(task_queue_) = false;
    185 
    186  ScopedTaskSafety safety_;
    187  TaskQueueBase* task_queue_;
    188 };
    189 }  // namespace webrtc
    190 #endif  // MODULES_PACING_TASK_QUEUE_PACED_SENDER_H_