rtp_sender_egress.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_RTP_RTCP_SOURCE_RTP_SENDER_EGRESS_H_
  11. #define MODULES_RTP_RTCP_SOURCE_RTP_SENDER_EGRESS_H_
  12. #include <map>
  13. #include <memory>
  14. #include <utility>
  15. #include <vector>
  16. #include "absl/types/optional.h"
  17. #include "api/call/transport.h"
  18. #include "api/rtc_event_log/rtc_event_log.h"
  19. #include "api/task_queue/task_queue_base.h"
  20. #include "api/units/data_rate.h"
  21. #include "modules/remote_bitrate_estimator/test/bwe_test_logging.h"
  22. #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
  23. #include "modules/rtp_rtcp/source/rtp_packet_history.h"
  24. #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
  25. #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h"
  26. #include "modules/rtp_rtcp/source/rtp_sequence_number_map.h"
  27. #include "rtc_base/rate_statistics.h"
  28. #include "rtc_base/synchronization/mutex.h"
  29. #include "rtc_base/synchronization/sequence_checker.h"
  30. #include "rtc_base/system/no_unique_address.h"
  31. #include "rtc_base/task_utils/pending_task_safety_flag.h"
  32. #include "rtc_base/task_utils/repeating_task.h"
  33. #include "rtc_base/thread_annotations.h"
  34. namespace webrtc {
  35. class RtpSenderEgress {
  36. public:
  37. // Helper class that redirects packets directly to the send part of this class
  38. // without passing through an actual paced sender.
  39. class NonPacedPacketSender : public RtpPacketSender {
  40. public:
  41. NonPacedPacketSender(RtpSenderEgress* sender,
  42. SequenceNumberAssigner* sequence_number_assigner);
  43. virtual ~NonPacedPacketSender();
  44. void EnqueuePackets(
  45. std::vector<std::unique_ptr<RtpPacketToSend>> packets) override;
  46. private:
  47. void PrepareForSend(RtpPacketToSend* packet);
  48. uint16_t transport_sequence_number_;
  49. RtpSenderEgress* const sender_;
  50. SequenceNumberAssigner* sequence_number_assigner_;
  51. };
  52. RtpSenderEgress(const RtpRtcpInterface::Configuration& config,
  53. RtpPacketHistory* packet_history);
  54. ~RtpSenderEgress();
  55. void SendPacket(RtpPacketToSend* packet, const PacedPacketInfo& pacing_info)
  56. RTC_LOCKS_EXCLUDED(lock_);
  57. uint32_t Ssrc() const { return ssrc_; }
  58. absl::optional<uint32_t> RtxSsrc() const { return rtx_ssrc_; }
  59. absl::optional<uint32_t> FlexFecSsrc() const { return flexfec_ssrc_; }
  60. RtpSendRates GetSendRates() const RTC_LOCKS_EXCLUDED(lock_);
  61. void GetDataCounters(StreamDataCounters* rtp_stats,
  62. StreamDataCounters* rtx_stats) const
  63. RTC_LOCKS_EXCLUDED(lock_);
  64. void ForceIncludeSendPacketsInAllocation(bool part_of_allocation)
  65. RTC_LOCKS_EXCLUDED(lock_);
  66. bool MediaHasBeenSent() const RTC_LOCKS_EXCLUDED(lock_);
  67. void SetMediaHasBeenSent(bool media_sent) RTC_LOCKS_EXCLUDED(lock_);
  68. void SetTimestampOffset(uint32_t timestamp) RTC_LOCKS_EXCLUDED(lock_);
  69. // For each sequence number in |sequence_number|, recall the last RTP packet
  70. // which bore it - its timestamp and whether it was the first and/or last
  71. // packet in that frame. If all of the given sequence numbers could be
  72. // recalled, return a vector with all of them (in corresponding order).
  73. // If any could not be recalled, return an empty vector.
  74. std::vector<RtpSequenceNumberMap::Info> GetSentRtpPacketInfos(
  75. rtc::ArrayView<const uint16_t> sequence_numbers) const
  76. RTC_LOCKS_EXCLUDED(lock_);
  77. void SetFecProtectionParameters(const FecProtectionParams& delta_params,
  78. const FecProtectionParams& key_params);
  79. std::vector<std::unique_ptr<RtpPacketToSend>> FetchFecPackets();
  80. private:
  81. // Maps capture time in milliseconds to send-side delay in milliseconds.
  82. // Send-side delay is the difference between transmission time and capture
  83. // time.
  84. typedef std::map<int64_t, int> SendDelayMap;
  85. RtpSendRates GetSendRatesLocked(int64_t now_ms) const
  86. RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
  87. bool HasCorrectSsrc(const RtpPacketToSend& packet) const;
  88. void AddPacketToTransportFeedback(uint16_t packet_id,
  89. const RtpPacketToSend& packet,
  90. const PacedPacketInfo& pacing_info);
  91. void UpdateDelayStatistics(int64_t capture_time_ms,
  92. int64_t now_ms,
  93. uint32_t ssrc);
  94. void RecomputeMaxSendDelay() RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
  95. void UpdateOnSendPacket(int packet_id,
  96. int64_t capture_time_ms,
  97. uint32_t ssrc);
  98. // Sends packet on to |transport_|, leaving the RTP module.
  99. bool SendPacketToNetwork(const RtpPacketToSend& packet,
  100. const PacketOptions& options,
  101. const PacedPacketInfo& pacing_info);
  102. void UpdateRtpStats(int64_t now_ms,
  103. uint32_t packet_ssrc,
  104. RtpPacketMediaType packet_type,
  105. RtpPacketCounter counter,
  106. size_t packet_size);
  107. #if BWE_TEST_LOGGING_COMPILE_TIME_ENABLE
  108. void BweTestLoggingPlot(int64_t now_ms, uint32_t packet_ssrc);
  109. #endif
  110. // Called on a timer, once a second, on the worker_queue_.
  111. void PeriodicUpdate();
  112. TaskQueueBase* const worker_queue_;
  113. RTC_NO_UNIQUE_ADDRESS SequenceChecker pacer_checker_;
  114. const uint32_t ssrc_;
  115. const absl::optional<uint32_t> rtx_ssrc_;
  116. const absl::optional<uint32_t> flexfec_ssrc_;
  117. const bool populate_network2_timestamp_;
  118. const bool send_side_bwe_with_overhead_;
  119. Clock* const clock_;
  120. RtpPacketHistory* const packet_history_;
  121. Transport* const transport_;
  122. RtcEventLog* const event_log_;
  123. #if BWE_TEST_LOGGING_COMPILE_TIME_ENABLE
  124. const bool is_audio_;
  125. #endif
  126. const bool need_rtp_packet_infos_;
  127. VideoFecGenerator* const fec_generator_ RTC_GUARDED_BY(pacer_checker_);
  128. TransportFeedbackObserver* const transport_feedback_observer_;
  129. SendSideDelayObserver* const send_side_delay_observer_;
  130. SendPacketObserver* const send_packet_observer_;
  131. StreamDataCountersCallback* const rtp_stats_callback_;
  132. BitrateStatisticsObserver* const bitrate_callback_;
  133. mutable Mutex lock_;
  134. bool media_has_been_sent_ RTC_GUARDED_BY(pacer_checker_);
  135. bool force_part_of_allocation_ RTC_GUARDED_BY(lock_);
  136. uint32_t timestamp_offset_ RTC_GUARDED_BY(worker_queue_);
  137. SendDelayMap send_delays_ RTC_GUARDED_BY(lock_);
  138. SendDelayMap::const_iterator max_delay_it_ RTC_GUARDED_BY(lock_);
  139. // The sum of delays over a kSendSideDelayWindowMs sliding window.
  140. int64_t sum_delays_ms_ RTC_GUARDED_BY(lock_);
  141. uint64_t total_packet_send_delay_ms_ RTC_GUARDED_BY(lock_);
  142. StreamDataCounters rtp_stats_ RTC_GUARDED_BY(lock_);
  143. StreamDataCounters rtx_rtp_stats_ RTC_GUARDED_BY(lock_);
  144. // One element per value in RtpPacketMediaType, with index matching value.
  145. std::vector<RateStatistics> send_rates_ RTC_GUARDED_BY(lock_);
  146. absl::optional<std::pair<FecProtectionParams, FecProtectionParams>>
  147. pending_fec_params_ RTC_GUARDED_BY(lock_);
  148. // Maps sent packets' sequence numbers to a tuple consisting of:
  149. // 1. The timestamp, without the randomizing offset mandated by the RFC.
  150. // 2. Whether the packet was the first in its frame.
  151. // 3. Whether the packet was the last in its frame.
  152. const std::unique_ptr<RtpSequenceNumberMap> rtp_sequence_number_map_
  153. RTC_GUARDED_BY(worker_queue_);
  154. RepeatingTaskHandle update_task_ RTC_GUARDED_BY(worker_queue_);
  155. ScopedTaskSafety task_safety_;
  156. };
  157. } // namespace webrtc
  158. #endif // MODULES_RTP_RTCP_SOURCE_RTP_SENDER_EGRESS_H_