task_queue_paced_sender.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef MODULES_PACING_TASK_QUEUE_PACED_SENDER_H_
  11. #define MODULES_PACING_TASK_QUEUE_PACED_SENDER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <functional>
  15. #include <memory>
  16. #include <queue>
  17. #include <vector>
  18. #include "absl/types/optional.h"
  19. #include "api/task_queue/task_queue_factory.h"
  20. #include "api/units/data_size.h"
  21. #include "api/units/time_delta.h"
  22. #include "api/units/timestamp.h"
  23. #include "modules/include/module.h"
  24. #include "modules/pacing/pacing_controller.h"
  25. #include "modules/pacing/packet_router.h"
  26. #include "modules/pacing/rtp_packet_pacer.h"
  27. #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
  28. #include "rtc_base/synchronization/mutex.h"
  29. #include "rtc_base/synchronization/sequence_checker.h"
  30. #include "rtc_base/task_queue.h"
  31. #include "rtc_base/thread_annotations.h"
  32. namespace webrtc {
  33. class Clock;
  34. class RtcEventLog;
  35. class TaskQueuePacedSender : public RtpPacketPacer, public RtpPacketSender {
  36. public:
  37. // The |hold_back_window| parameter sets a lower bound on time to sleep if
  38. // there is currently a pacer queue and packets can't immediately be
  39. // processed. Increasing this reduces thread wakeups at the expense of higher
  40. // latency.
  41. // TODO(bugs.webrtc.org/10809): Remove default value for hold_back_window.
  42. TaskQueuePacedSender(
  43. Clock* clock,
  44. PacketRouter* packet_router,
  45. RtcEventLog* event_log,
  46. const WebRtcKeyValueConfig* field_trials,
  47. TaskQueueFactory* task_queue_factory,
  48. TimeDelta hold_back_window = PacingController::kMinSleepTime);
  49. ~TaskQueuePacedSender() override;
  50. // Methods implementing RtpPacketSender.
  51. // Adds the packet to the queue and calls PacketRouter::SendPacket() when
  52. // it's time to send.
  53. void EnqueuePackets(
  54. std::vector<std::unique_ptr<RtpPacketToSend>> packets) override;
  55. // Methods implementing RtpPacketPacer:
  56. void CreateProbeCluster(DataRate bitrate, int cluster_id) override;
  57. // Temporarily pause all sending.
  58. void Pause() override;
  59. // Resume sending packets.
  60. void Resume() override;
  61. void SetCongestionWindow(DataSize congestion_window_size) override;
  62. void UpdateOutstandingData(DataSize outstanding_data) override;
  63. // Sets the pacing rates. Must be called once before packets can be sent.
  64. void SetPacingRates(DataRate pacing_rate, DataRate padding_rate) override;
  65. // Currently audio traffic is not accounted for by pacer and passed through.
  66. // With the introduction of audio BWE, audio traffic will be accounted for
  67. // in the pacer budget calculation. The audio traffic will still be injected
  68. // at high priority.
  69. void SetAccountForAudioPackets(bool account_for_audio) override;
  70. void SetIncludeOverhead() override;
  71. void SetTransportOverhead(DataSize overhead_per_packet) override;
  72. // Returns the time since the oldest queued packet was enqueued.
  73. TimeDelta OldestPacketWaitTime() const override;
  74. // Returns total size of all packets in the pacer queue.
  75. DataSize QueueSizeData() const override;
  76. // Returns the time when the first packet was sent;
  77. absl::optional<Timestamp> FirstSentPacketTime() const override;
  78. // Returns the number of milliseconds it will take to send the current
  79. // packets in the queue, given the current size and bitrate, ignoring prio.
  80. TimeDelta ExpectedQueueTime() const override;
  81. // Set the max desired queuing delay, pacer will override the pacing rate
  82. // specified by SetPacingRates() if needed to achieve this goal.
  83. void SetQueueTimeLimit(TimeDelta limit) override;
  84. protected:
  85. // Exposed as protected for test.
  86. struct Stats {
  87. Stats()
  88. : oldest_packet_wait_time(TimeDelta::Zero()),
  89. queue_size(DataSize::Zero()),
  90. expected_queue_time(TimeDelta::Zero()) {}
  91. TimeDelta oldest_packet_wait_time;
  92. DataSize queue_size;
  93. TimeDelta expected_queue_time;
  94. absl::optional<Timestamp> first_sent_packet_time;
  95. };
  96. virtual void OnStatsUpdated(const Stats& stats);
  97. private:
  98. // Check if it is time to send packets, or schedule a delayed task if not.
  99. // Use Timestamp::MinusInfinity() to indicate that this call has _not_
  100. // been scheduled by the pacing controller. If this is the case, check if
  101. // can execute immediately otherwise schedule a delay task that calls this
  102. // method again with desired (finite) scheduled process time.
  103. void MaybeProcessPackets(Timestamp scheduled_process_time);
  104. void MaybeUpdateStats(bool is_scheduled_call) RTC_RUN_ON(task_queue_);
  105. Stats GetStats() const;
  106. Clock* const clock_;
  107. const TimeDelta hold_back_window_;
  108. PacingController pacing_controller_ RTC_GUARDED_BY(task_queue_);
  109. // We want only one (valid) delayed process task in flight at a time.
  110. // If the value of |next_process_time_| is finite, it is an id for a
  111. // delayed task that will call MaybeProcessPackets() with that time
  112. // as parameter.
  113. // Timestamp::MinusInfinity() indicates no valid pending task.
  114. Timestamp next_process_time_ RTC_GUARDED_BY(task_queue_);
  115. // Since we don't want to support synchronous calls that wait for a
  116. // task execution, we poll the stats at some interval and update
  117. // |current_stats_|, which can in turn be polled at any time.
  118. // True iff there is delayed task in flight that that will call
  119. // UdpateStats().
  120. bool stats_update_scheduled_ RTC_GUARDED_BY(task_queue_);
  121. // Last time stats were updated.
  122. Timestamp last_stats_time_ RTC_GUARDED_BY(task_queue_);
  123. // Indicates if this task queue is shutting down. If so, don't allow
  124. // posting any more delayed tasks as that can cause the task queue to
  125. // never drain.
  126. bool is_shutdown_ RTC_GUARDED_BY(task_queue_);
  127. mutable Mutex stats_mutex_;
  128. Stats current_stats_ RTC_GUARDED_BY(stats_mutex_);
  129. rtc::TaskQueue task_queue_;
  130. };
  131. } // namespace webrtc
  132. #endif // MODULES_PACING_TASK_QUEUE_PACED_SENDER_H_