You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
6.3 KiB

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