paced_sender.h 6.1 KB

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