composite_rtp_transport.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 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 PC_COMPOSITE_RTP_TRANSPORT_H_
  11. #define PC_COMPOSITE_RTP_TRANSPORT_H_
  12. #include <memory>
  13. #include <set>
  14. #include <string>
  15. #include <vector>
  16. #include "call/rtp_demuxer.h"
  17. #include "call/rtp_packet_sink_interface.h"
  18. #include "pc/rtp_transport_internal.h"
  19. #include "pc/session_description.h"
  20. #include "rtc_base/async_packet_socket.h"
  21. #include "rtc_base/copy_on_write_buffer.h"
  22. namespace webrtc {
  23. // Composite RTP transport capable of receiving from multiple sub-transports.
  24. //
  25. // CompositeRtpTransport is receive-only until the caller explicitly chooses
  26. // which transport will be used to send and calls |SetSendTransport|. This
  27. // choice must be made as part of the SDP negotiation process, based on receipt
  28. // of a provisional answer. |CompositeRtpTransport| does not become writable or
  29. // ready to send until |SetSendTransport| is called.
  30. //
  31. // When a full answer is received, the user should replace the composite
  32. // transport with the single, chosen RTP transport, then delete the composite
  33. // and all non-chosen transports.
  34. class CompositeRtpTransport : public RtpTransportInternal {
  35. public:
  36. // Constructs a composite out of the given |transports|. |transports| must
  37. // not be empty. All |transports| must outlive the composite.
  38. explicit CompositeRtpTransport(std::vector<RtpTransportInternal*> transports);
  39. // Sets which transport will be used for sending packets. Once called,
  40. // |IsReadyToSend|, |IsWritable|, and the associated signals will reflect the
  41. // state of |send_tranpsort|.
  42. void SetSendTransport(RtpTransportInternal* send_transport);
  43. // Removes |transport| from the composite. No-op if |transport| is null or
  44. // not found in the composite. Removing a transport disconnects all signals
  45. // and RTP demux sinks from that transport. The send transport may not be
  46. // removed.
  47. void RemoveTransport(RtpTransportInternal* transport);
  48. // All transports within a composite must have the same name.
  49. const std::string& transport_name() const override;
  50. int SetRtpOption(rtc::Socket::Option opt, int value) override;
  51. int SetRtcpOption(rtc::Socket::Option opt, int value) override;
  52. // All transports within a composite must either enable or disable RTCP mux.
  53. bool rtcp_mux_enabled() const override;
  54. // Enables or disables RTCP mux for all component transports.
  55. void SetRtcpMuxEnabled(bool enabled) override;
  56. // The composite is ready to send if |send_transport_| is set and ready to
  57. // send.
  58. bool IsReadyToSend() const override;
  59. // The composite is writable if |send_transport_| is set and writable.
  60. bool IsWritable(bool rtcp) const override;
  61. // Sends an RTP packet. May only be called after |send_transport_| is set.
  62. bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet,
  63. const rtc::PacketOptions& options,
  64. int flags) override;
  65. // Sends an RTCP packet. May only be called after |send_transport_| is set.
  66. bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet,
  67. const rtc::PacketOptions& options,
  68. int flags) override;
  69. // Updates the mapping of RTP header extensions for all component transports.
  70. void UpdateRtpHeaderExtensionMap(
  71. const cricket::RtpHeaderExtensions& header_extensions) override;
  72. // SRTP is only active for a composite if it is active for all component
  73. // transports.
  74. bool IsSrtpActive() const override;
  75. // Registers an RTP demux sink with all component transports.
  76. bool RegisterRtpDemuxerSink(const RtpDemuxerCriteria& criteria,
  77. RtpPacketSinkInterface* sink) override;
  78. bool UnregisterRtpDemuxerSink(RtpPacketSinkInterface* sink) override;
  79. private:
  80. // Receive-side signals.
  81. void OnNetworkRouteChanged(absl::optional<rtc::NetworkRoute> route);
  82. void OnRtcpPacketReceived(rtc::CopyOnWriteBuffer* packet,
  83. int64_t packet_time_us);
  84. // Send-side signals.
  85. void OnWritableState(bool writable);
  86. void OnReadyToSend(bool ready_to_send);
  87. void OnSentPacket(const rtc::SentPacket& packet);
  88. std::vector<RtpTransportInternal*> transports_;
  89. RtpTransportInternal* send_transport_ = nullptr;
  90. // Record of registered RTP demuxer sinks. Used to unregister sinks when a
  91. // transport is removed.
  92. std::set<RtpPacketSinkInterface*> rtp_demuxer_sinks_;
  93. };
  94. } // namespace webrtc
  95. #endif // PC_COMPOSITE_RTP_TRANSPORT_H_