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.

178 lines
5.6 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. #include "modules/pacing/paced_sender.h"
  11. #include <algorithm>
  12. #include <utility>
  13. #include <vector>
  14. #include "absl/memory/memory.h"
  15. #include "api/rtc_event_log/rtc_event_log.h"
  16. #include "modules/utility/include/process_thread.h"
  17. #include "rtc_base/checks.h"
  18. #include "rtc_base/location.h"
  19. #include "rtc_base/logging.h"
  20. #include "rtc_base/time_utils.h"
  21. #include "system_wrappers/include/clock.h"
  22. namespace webrtc {
  23. const int64_t PacedSender::kMaxQueueLengthMs = 2000;
  24. const float PacedSender::kDefaultPaceMultiplier = 2.5f;
  25. PacedSender::PacedSender(Clock* clock,
  26. PacketRouter* packet_router,
  27. RtcEventLog* event_log,
  28. const WebRtcKeyValueConfig* field_trials,
  29. ProcessThread* process_thread)
  30. : pacing_controller_(clock,
  31. static_cast<PacingController::PacketSender*>(this),
  32. event_log,
  33. field_trials),
  34. packet_router_(packet_router),
  35. process_thread_(process_thread) {
  36. if (process_thread_)
  37. process_thread_->RegisterModule(&module_proxy_, RTC_FROM_HERE);
  38. }
  39. PacedSender::~PacedSender() {
  40. if (process_thread_)
  41. process_thread_->DeRegisterModule(&module_proxy_);
  42. }
  43. void PacedSender::CreateProbeCluster(DataRate bitrate, int cluster_id) {
  44. rtc::CritScope cs(&critsect_);
  45. return pacing_controller_.CreateProbeCluster(bitrate, cluster_id);
  46. }
  47. void PacedSender::Pause() {
  48. {
  49. rtc::CritScope cs(&critsect_);
  50. pacing_controller_.Pause();
  51. }
  52. // Tell the process thread to call our TimeUntilNextProcess() method to get
  53. // a new (longer) estimate for when to call Process().
  54. if (process_thread_)
  55. process_thread_->WakeUp(&module_proxy_);
  56. }
  57. void PacedSender::Resume() {
  58. {
  59. rtc::CritScope cs(&critsect_);
  60. pacing_controller_.Resume();
  61. }
  62. // Tell the process thread to call our TimeUntilNextProcess() method to
  63. // refresh the estimate for when to call Process().
  64. if (process_thread_)
  65. process_thread_->WakeUp(&module_proxy_);
  66. }
  67. void PacedSender::SetCongestionWindow(DataSize congestion_window_size) {
  68. rtc::CritScope cs(&critsect_);
  69. pacing_controller_.SetCongestionWindow(congestion_window_size);
  70. }
  71. void PacedSender::UpdateOutstandingData(DataSize outstanding_data) {
  72. rtc::CritScope cs(&critsect_);
  73. pacing_controller_.UpdateOutstandingData(outstanding_data);
  74. }
  75. void PacedSender::SetPacingRates(DataRate pacing_rate, DataRate padding_rate) {
  76. rtc::CritScope cs(&critsect_);
  77. pacing_controller_.SetPacingRates(pacing_rate, padding_rate);
  78. }
  79. void PacedSender::EnqueuePackets(
  80. std::vector<std::unique_ptr<RtpPacketToSend>> packets) {
  81. rtc::CritScope cs(&critsect_);
  82. for (auto& packet : packets) {
  83. pacing_controller_.EnqueuePacket(std::move(packet));
  84. }
  85. }
  86. void PacedSender::SetAccountForAudioPackets(bool account_for_audio) {
  87. rtc::CritScope cs(&critsect_);
  88. pacing_controller_.SetAccountForAudioPackets(account_for_audio);
  89. }
  90. TimeDelta PacedSender::ExpectedQueueTime() const {
  91. rtc::CritScope cs(&critsect_);
  92. return pacing_controller_.ExpectedQueueTime();
  93. }
  94. DataSize PacedSender::QueueSizeData() const {
  95. rtc::CritScope cs(&critsect_);
  96. return pacing_controller_.QueueSizeData();
  97. }
  98. absl::optional<Timestamp> PacedSender::FirstSentPacketTime() const {
  99. rtc::CritScope cs(&critsect_);
  100. return pacing_controller_.FirstSentPacketTime();
  101. }
  102. TimeDelta PacedSender::OldestPacketWaitTime() const {
  103. rtc::CritScope cs(&critsect_);
  104. return pacing_controller_.OldestPacketWaitTime();
  105. }
  106. int64_t PacedSender::TimeUntilNextProcess() {
  107. rtc::CritScope cs(&critsect_);
  108. // When paused we wake up every 500 ms to send a padding packet to ensure
  109. // we won't get stuck in the paused state due to no feedback being received.
  110. TimeDelta elapsed_time = pacing_controller_.TimeElapsedSinceLastProcess();
  111. if (pacing_controller_.IsPaused()) {
  112. return std::max(PacingController::kPausedProcessInterval - elapsed_time,
  113. TimeDelta::Zero())
  114. .ms();
  115. }
  116. auto next_probe = pacing_controller_.TimeUntilNextProbe();
  117. if (next_probe) {
  118. return next_probe->ms();
  119. }
  120. const TimeDelta min_packet_limit = TimeDelta::ms(5);
  121. return std::max(min_packet_limit - elapsed_time, TimeDelta::Zero()).ms();
  122. }
  123. void PacedSender::Process() {
  124. rtc::CritScope cs(&critsect_);
  125. pacing_controller_.ProcessPackets();
  126. }
  127. void PacedSender::ProcessThreadAttached(ProcessThread* process_thread) {
  128. RTC_LOG(LS_INFO) << "ProcessThreadAttached 0x" << process_thread;
  129. RTC_DCHECK(!process_thread || process_thread == process_thread_);
  130. }
  131. void PacedSender::SetQueueTimeLimit(TimeDelta limit) {
  132. rtc::CritScope cs(&critsect_);
  133. pacing_controller_.SetQueueTimeLimit(limit);
  134. }
  135. void PacedSender::SendRtpPacket(std::unique_ptr<RtpPacketToSend> packet,
  136. const PacedPacketInfo& cluster_info) {
  137. critsect_.Leave();
  138. packet_router_->SendPacket(std::move(packet), cluster_info);
  139. critsect_.Enter();
  140. }
  141. std::vector<std::unique_ptr<RtpPacketToSend>> PacedSender::GeneratePadding(
  142. DataSize size) {
  143. std::vector<std::unique_ptr<RtpPacketToSend>> padding_packets;
  144. critsect_.Leave();
  145. padding_packets = packet_router_->GeneratePadding(size.bytes());
  146. critsect_.Enter();
  147. return padding_packets;
  148. }
  149. } // namespace webrtc