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