packet_router.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2015 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_PACKET_ROUTER_H_
  11. #define MODULES_PACING_PACKET_ROUTER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <list>
  15. #include <memory>
  16. #include <unordered_map>
  17. #include <utility>
  18. #include <vector>
  19. #include "api/transport/network_types.h"
  20. #include "modules/pacing/pacing_controller.h"
  21. #include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
  22. #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
  23. #include "modules/rtp_rtcp/source/rtcp_packet.h"
  24. #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
  25. #include "rtc_base/constructor_magic.h"
  26. #include "rtc_base/synchronization/mutex.h"
  27. #include "rtc_base/thread_annotations.h"
  28. namespace webrtc {
  29. class RtpRtcpInterface;
  30. // PacketRouter keeps track of rtp send modules to support the pacer.
  31. // In addition, it handles feedback messages, which are sent on a send
  32. // module if possible (sender report), otherwise on receive module
  33. // (receiver report). For the latter case, we also keep track of the
  34. // receive modules.
  35. class PacketRouter : public RemoteBitrateObserver,
  36. public TransportFeedbackSenderInterface,
  37. public PacingController::PacketSender {
  38. public:
  39. PacketRouter();
  40. explicit PacketRouter(uint16_t start_transport_seq);
  41. ~PacketRouter() override;
  42. void AddSendRtpModule(RtpRtcpInterface* rtp_module, bool remb_candidate);
  43. void RemoveSendRtpModule(RtpRtcpInterface* rtp_module);
  44. void AddReceiveRtpModule(RtcpFeedbackSenderInterface* rtcp_sender,
  45. bool remb_candidate);
  46. void RemoveReceiveRtpModule(RtcpFeedbackSenderInterface* rtcp_sender);
  47. void SendPacket(std::unique_ptr<RtpPacketToSend> packet,
  48. const PacedPacketInfo& cluster_info) override;
  49. std::vector<std::unique_ptr<RtpPacketToSend>> FetchFec() override;
  50. std::vector<std::unique_ptr<RtpPacketToSend>> GeneratePadding(
  51. DataSize size) override;
  52. uint16_t CurrentTransportSequenceNumber() const;
  53. // Called every time there is a new bitrate estimate for a receive channel
  54. // group. This call will trigger a new RTCP REMB packet if the bitrate
  55. // estimate has decreased or if no RTCP REMB packet has been sent for
  56. // a certain time interval.
  57. // Implements RtpReceiveBitrateUpdate.
  58. void OnReceiveBitrateChanged(const std::vector<uint32_t>& ssrcs,
  59. uint32_t bitrate_bps) override;
  60. // Ensures remote party notified of the receive bitrate limit no larger than
  61. // |bitrate_bps|.
  62. void SetMaxDesiredReceiveBitrate(int64_t bitrate_bps);
  63. // Send REMB feedback.
  64. bool SendRemb(int64_t bitrate_bps, const std::vector<uint32_t>& ssrcs);
  65. // Sends |packets| in one or more IP packets.
  66. bool SendCombinedRtcpPacket(
  67. std::vector<std::unique_ptr<rtcp::RtcpPacket>> packets) override;
  68. private:
  69. void AddRembModuleCandidate(RtcpFeedbackSenderInterface* candidate_module,
  70. bool media_sender)
  71. RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
  72. void MaybeRemoveRembModuleCandidate(
  73. RtcpFeedbackSenderInterface* candidate_module,
  74. bool media_sender) RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
  75. void UnsetActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
  76. void DetermineActiveRembModule() RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
  77. void AddSendRtpModuleToMap(RtpRtcpInterface* rtp_module, uint32_t ssrc)
  78. RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
  79. void RemoveSendRtpModuleFromMap(uint32_t ssrc)
  80. RTC_EXCLUSIVE_LOCKS_REQUIRED(modules_mutex_);
  81. mutable Mutex modules_mutex_;
  82. // Ssrc to RtpRtcpInterface module;
  83. std::unordered_map<uint32_t, RtpRtcpInterface*> send_modules_map_
  84. RTC_GUARDED_BY(modules_mutex_);
  85. std::list<RtpRtcpInterface*> send_modules_list_
  86. RTC_GUARDED_BY(modules_mutex_);
  87. // The last module used to send media.
  88. RtpRtcpInterface* last_send_module_ RTC_GUARDED_BY(modules_mutex_);
  89. // Rtcp modules of the rtp receivers.
  90. std::vector<RtcpFeedbackSenderInterface*> rtcp_feedback_senders_
  91. RTC_GUARDED_BY(modules_mutex_);
  92. // TODO(eladalon): remb_mutex_ only ever held from one function, and it's not
  93. // clear if that function can actually be called from more than one thread.
  94. Mutex remb_mutex_;
  95. // The last time a REMB was sent.
  96. int64_t last_remb_time_ms_ RTC_GUARDED_BY(remb_mutex_);
  97. int64_t last_send_bitrate_bps_ RTC_GUARDED_BY(remb_mutex_);
  98. // The last bitrate update.
  99. int64_t bitrate_bps_ RTC_GUARDED_BY(remb_mutex_);
  100. int64_t max_bitrate_bps_ RTC_GUARDED_BY(remb_mutex_);
  101. // Candidates for the REMB module can be RTP sender/receiver modules, with
  102. // the sender modules taking precedence.
  103. std::vector<RtcpFeedbackSenderInterface*> sender_remb_candidates_
  104. RTC_GUARDED_BY(modules_mutex_);
  105. std::vector<RtcpFeedbackSenderInterface*> receiver_remb_candidates_
  106. RTC_GUARDED_BY(modules_mutex_);
  107. RtcpFeedbackSenderInterface* active_remb_module_
  108. RTC_GUARDED_BY(modules_mutex_);
  109. uint64_t transport_seq_ RTC_GUARDED_BY(modules_mutex_);
  110. // TODO(bugs.webrtc.org/10809): Replace lock with a sequence checker once the
  111. // process thread is gone.
  112. std::vector<std::unique_ptr<RtpPacketToSend>> pending_fec_packets_
  113. RTC_GUARDED_BY(modules_mutex_);
  114. RTC_DISALLOW_COPY_AND_ASSIGN(PacketRouter);
  115. };
  116. } // namespace webrtc
  117. #endif // MODULES_PACING_PACKET_ROUTER_H_