rtp_data_channel.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2020 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_RTP_DATA_CHANNEL_H_
  11. #define PC_RTP_DATA_CHANNEL_H_
  12. #include <memory>
  13. #include <string>
  14. #include "api/data_channel_interface.h"
  15. #include "api/priority.h"
  16. #include "api/scoped_refptr.h"
  17. #include "api/transport/data_channel_transport_interface.h"
  18. #include "media/base/media_channel.h"
  19. #include "pc/channel.h"
  20. #include "pc/data_channel_utils.h"
  21. #include "rtc_base/async_invoker.h"
  22. #include "rtc_base/third_party/sigslot/sigslot.h"
  23. namespace webrtc {
  24. class RtpDataChannel;
  25. // TODO(deadbeef): Once RTP data channels go away, get rid of this and have
  26. // DataChannel depend on SctpTransportInternal (pure virtual SctpTransport
  27. // interface) instead.
  28. class RtpDataChannelProviderInterface {
  29. public:
  30. // Sends the data to the transport.
  31. virtual bool SendData(const cricket::SendDataParams& params,
  32. const rtc::CopyOnWriteBuffer& payload,
  33. cricket::SendDataResult* result) = 0;
  34. // Connects to the transport signals.
  35. virtual bool ConnectDataChannel(RtpDataChannel* data_channel) = 0;
  36. // Disconnects from the transport signals.
  37. virtual void DisconnectDataChannel(RtpDataChannel* data_channel) = 0;
  38. // Returns true if the transport channel is ready to send data.
  39. virtual bool ReadyToSendData() const = 0;
  40. protected:
  41. virtual ~RtpDataChannelProviderInterface() {}
  42. };
  43. // RtpDataChannel is an implementation of the DataChannelInterface based on
  44. // libjingle's data engine. It provides an implementation of unreliable data
  45. // channels.
  46. // DataChannel states:
  47. // kConnecting: The channel has been created the transport might not yet be
  48. // ready.
  49. // kOpen: The channel have a local SSRC set by a call to UpdateSendSsrc
  50. // and a remote SSRC set by call to UpdateReceiveSsrc and the transport
  51. // has been writable once.
  52. // kClosing: DataChannelInterface::Close has been called or UpdateReceiveSsrc
  53. // has been called with SSRC==0
  54. // kClosed: Both UpdateReceiveSsrc and UpdateSendSsrc has been called with
  55. // SSRC==0.
  56. class RtpDataChannel : public DataChannelInterface,
  57. public sigslot::has_slots<> {
  58. public:
  59. static rtc::scoped_refptr<RtpDataChannel> Create(
  60. RtpDataChannelProviderInterface* provider,
  61. const std::string& label,
  62. const DataChannelInit& config,
  63. rtc::Thread* signaling_thread);
  64. // Instantiates an API proxy for a DataChannel instance that will be handed
  65. // out to external callers.
  66. static rtc::scoped_refptr<DataChannelInterface> CreateProxy(
  67. rtc::scoped_refptr<RtpDataChannel> channel);
  68. void RegisterObserver(DataChannelObserver* observer) override;
  69. void UnregisterObserver() override;
  70. std::string label() const override { return label_; }
  71. bool reliable() const override { return false; }
  72. bool ordered() const override { return config_.ordered; }
  73. // Backwards compatible accessors
  74. uint16_t maxRetransmitTime() const override {
  75. return config_.maxRetransmitTime ? *config_.maxRetransmitTime
  76. : static_cast<uint16_t>(-1);
  77. }
  78. uint16_t maxRetransmits() const override {
  79. return config_.maxRetransmits ? *config_.maxRetransmits
  80. : static_cast<uint16_t>(-1);
  81. }
  82. absl::optional<int> maxPacketLifeTime() const override {
  83. return config_.maxRetransmitTime;
  84. }
  85. absl::optional<int> maxRetransmitsOpt() const override {
  86. return config_.maxRetransmits;
  87. }
  88. std::string protocol() const override { return config_.protocol; }
  89. bool negotiated() const override { return config_.negotiated; }
  90. int id() const override { return config_.id; }
  91. Priority priority() const override {
  92. return config_.priority ? *config_.priority : Priority::kLow;
  93. }
  94. virtual int internal_id() const { return internal_id_; }
  95. uint64_t buffered_amount() const override { return 0; }
  96. void Close() override;
  97. DataState state() const override;
  98. RTCError error() const override;
  99. uint32_t messages_sent() const override;
  100. uint64_t bytes_sent() const override;
  101. uint32_t messages_received() const override;
  102. uint64_t bytes_received() const override;
  103. bool Send(const DataBuffer& buffer) override;
  104. // Close immediately, ignoring any queued data or closing procedure.
  105. // This is called when SDP indicates a channel should be removed.
  106. void CloseAbruptlyWithError(RTCError error);
  107. // Called when the channel's ready to use. That can happen when the
  108. // underlying DataMediaChannel becomes ready, or when this channel is a new
  109. // stream on an existing DataMediaChannel, and we've finished negotiation.
  110. void OnChannelReady(bool writable);
  111. // Slots for provider to connect signals to.
  112. void OnDataReceived(const cricket::ReceiveDataParams& params,
  113. const rtc::CopyOnWriteBuffer& payload);
  114. // Called when the transport channel is unusable.
  115. // This method makes sure the DataChannel is disconnected and changes state
  116. // to kClosed.
  117. void OnTransportChannelClosed();
  118. DataChannelStats GetStats() const;
  119. // The remote peer requested that this channel should be closed.
  120. void RemotePeerRequestClose();
  121. // Set the SSRC this channel should use to send data on the
  122. // underlying data engine. |send_ssrc| == 0 means that the channel is no
  123. // longer part of the session negotiation.
  124. void SetSendSsrc(uint32_t send_ssrc);
  125. // Set the SSRC this channel should use to receive data from the
  126. // underlying data engine.
  127. void SetReceiveSsrc(uint32_t receive_ssrc);
  128. // Emitted when state transitions to kOpen.
  129. sigslot::signal1<DataChannelInterface*> SignalOpened;
  130. // Emitted when state transitions to kClosed.
  131. sigslot::signal1<DataChannelInterface*> SignalClosed;
  132. // Reset the allocator for internal ID values for testing, so that
  133. // the internal IDs generated are predictable. Test only.
  134. static void ResetInternalIdAllocatorForTesting(int new_value);
  135. protected:
  136. RtpDataChannel(const DataChannelInit& config,
  137. RtpDataChannelProviderInterface* client,
  138. const std::string& label,
  139. rtc::Thread* signaling_thread);
  140. ~RtpDataChannel() override;
  141. private:
  142. bool Init();
  143. void UpdateState();
  144. void SetState(DataState state);
  145. void DisconnectFromProvider();
  146. void DeliverQueuedReceivedData();
  147. bool SendDataMessage(const DataBuffer& buffer);
  148. rtc::Thread* const signaling_thread_;
  149. const int internal_id_;
  150. const std::string label_;
  151. const DataChannelInit config_;
  152. DataChannelObserver* observer_ RTC_GUARDED_BY(signaling_thread_) = nullptr;
  153. DataState state_ RTC_GUARDED_BY(signaling_thread_) = kConnecting;
  154. RTCError error_ RTC_GUARDED_BY(signaling_thread_);
  155. uint32_t messages_sent_ RTC_GUARDED_BY(signaling_thread_) = 0;
  156. uint64_t bytes_sent_ RTC_GUARDED_BY(signaling_thread_) = 0;
  157. uint32_t messages_received_ RTC_GUARDED_BY(signaling_thread_) = 0;
  158. uint64_t bytes_received_ RTC_GUARDED_BY(signaling_thread_) = 0;
  159. RtpDataChannelProviderInterface* const provider_;
  160. bool connected_to_provider_ RTC_GUARDED_BY(signaling_thread_) = false;
  161. bool send_ssrc_set_ RTC_GUARDED_BY(signaling_thread_) = false;
  162. bool receive_ssrc_set_ RTC_GUARDED_BY(signaling_thread_) = false;
  163. bool writable_ RTC_GUARDED_BY(signaling_thread_) = false;
  164. uint32_t send_ssrc_ RTC_GUARDED_BY(signaling_thread_) = 0;
  165. uint32_t receive_ssrc_ RTC_GUARDED_BY(signaling_thread_) = 0;
  166. PacketQueue queued_received_data_ RTC_GUARDED_BY(signaling_thread_);
  167. rtc::AsyncInvoker invoker_ RTC_GUARDED_BY(signaling_thread_);
  168. };
  169. } // namespace webrtc
  170. #endif // PC_RTP_DATA_CHANNEL_H_