ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef PC_DATA_CHANNEL_H_ |
| 12 | #define PC_DATA_CHANNEL_H_ |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 13 | |
| 14 | #include <deque> |
Steve Anton | 944c755 | 2018-12-13 22:19:10 | [diff] [blame] | 15 | #include <memory> |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 16 | #include <set> |
| 17 | #include <string> |
| 18 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 19 | #include "api/data_channel_interface.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 20 | #include "api/proxy.h" |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 21 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 22 | #include "media/base/media_channel.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 23 | #include "pc/channel.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 24 | #include "rtc_base/async_invoker.h" |
Artem Titov | e41c433 | 2018-07-25 13:04:28 | [diff] [blame] | 25 | #include "rtc_base/third_party/sigslot/sigslot.h" |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | class DataChannel; |
| 30 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 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. |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 34 | class DataChannelProviderInterface { |
| 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(DataChannel* data_channel) = 0; |
| 42 | // Disconnects from the transport signals. |
| 43 | virtual void DisconnectDataChannel(DataChannel* data_channel) = 0; |
| 44 | // Adds the data channel SID to the transport for SCTP. |
| 45 | virtual void AddSctpDataStream(int sid) = 0; |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 46 | // Begins the closing procedure by sending an outgoing stream reset. Still |
| 47 | // need to wait for callbacks to tell when this completes. |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 48 | virtual void RemoveSctpDataStream(int sid) = 0; |
| 49 | // Returns true if the transport channel is ready to send data. |
| 50 | virtual bool ReadyToSendData() const = 0; |
| 51 | |
| 52 | protected: |
| 53 | virtual ~DataChannelProviderInterface() {} |
| 54 | }; |
| 55 | |
| 56 | struct InternalDataChannelInit : public DataChannelInit { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 57 | enum OpenHandshakeRole { kOpener, kAcker, kNone }; |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 58 | // The default role is kOpener because the default |negotiated| is false. |
| 59 | InternalDataChannelInit() : open_handshake_role(kOpener) {} |
Harald Alvestrand | f3736ed | 2019-04-08 11:09:30 | [diff] [blame] | 60 | explicit InternalDataChannelInit(const DataChannelInit& base); |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 61 | OpenHandshakeRole open_handshake_role; |
| 62 | }; |
| 63 | |
| 64 | // Helper class to allocate unique IDs for SCTP DataChannels |
| 65 | class SctpSidAllocator { |
| 66 | public: |
| 67 | // Gets the first unused odd/even id based on the DTLS role. If |role| is |
| 68 | // SSL_CLIENT, the allocated id starts from 0 and takes even numbers; |
| 69 | // otherwise, the id starts from 1 and takes odd numbers. |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 70 | // Returns false if no ID can be allocated. |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 71 | bool AllocateSid(rtc::SSLRole role, int* sid); |
| 72 | |
| 73 | // Attempts to reserve a specific sid. Returns false if it's unavailable. |
| 74 | bool ReserveSid(int sid); |
| 75 | |
| 76 | // Indicates that |sid| isn't in use any more, and is thus available again. |
| 77 | void ReleaseSid(int sid); |
| 78 | |
| 79 | private: |
| 80 | // Checks if |sid| is available to be assigned to a new SCTP data channel. |
| 81 | bool IsSidAvailable(int sid) const; |
| 82 | |
| 83 | std::set<int> used_sids_; |
| 84 | }; |
| 85 | |
| 86 | // DataChannel is a an implementation of the DataChannelInterface based on |
| 87 | // libjingle's data engine. It provides an implementation of unreliable or |
| 88 | // reliabledata channels. Currently this class is specifically designed to use |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 89 | // both RtpDataChannel and SctpTransport. |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 90 | |
| 91 | // DataChannel states: |
| 92 | // kConnecting: The channel has been created the transport might not yet be |
| 93 | // ready. |
| 94 | // kOpen: The channel have a local SSRC set by a call to UpdateSendSsrc |
| 95 | // and a remote SSRC set by call to UpdateReceiveSsrc and the transport |
| 96 | // has been writable once. |
| 97 | // kClosing: DataChannelInterface::Close has been called or UpdateReceiveSsrc |
| 98 | // has been called with SSRC==0 |
| 99 | // kClosed: Both UpdateReceiveSsrc and UpdateSendSsrc has been called with |
| 100 | // SSRC==0. |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 101 | // |
| 102 | // How the closing procedure works for SCTP: |
| 103 | // 1. Alice calls Close(), state changes to kClosing. |
| 104 | // 2. Alice finishes sending any queued data. |
| 105 | // 3. Alice calls RemoveSctpDataStream, sends outgoing stream reset. |
| 106 | // 4. Bob receives incoming stream reset; OnClosingProcedureStartedRemotely |
| 107 | // called. |
| 108 | // 5. Bob sends outgoing stream reset. 6. Alice receives incoming reset, |
| 109 | // Bob receives acknowledgement. Both receive OnClosingProcedureComplete |
| 110 | // callback and transition to kClosed. |
Steve Anton | 044a04d | 2018-08-31 20:51:19 | [diff] [blame] | 111 | class DataChannel : public DataChannelInterface, public sigslot::has_slots<> { |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 112 | public: |
| 113 | static rtc::scoped_refptr<DataChannel> Create( |
| 114 | DataChannelProviderInterface* provider, |
| 115 | cricket::DataChannelType dct, |
| 116 | const std::string& label, |
| 117 | const InternalDataChannelInit& config); |
| 118 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 119 | static bool IsSctpLike(cricket::DataChannelType type); |
| 120 | |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 121 | virtual void RegisterObserver(DataChannelObserver* observer); |
| 122 | virtual void UnregisterObserver(); |
| 123 | |
| 124 | virtual std::string label() const { return label_; } |
| 125 | virtual bool reliable() const; |
| 126 | virtual bool ordered() const { return config_.ordered; } |
Harald Alvestrand | f3736ed | 2019-04-08 11:09:30 | [diff] [blame] | 127 | // Backwards compatible accessors |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 128 | virtual uint16_t maxRetransmitTime() const { |
Harald Alvestrand | f3736ed | 2019-04-08 11:09:30 | [diff] [blame] | 129 | return config_.maxRetransmitTime ? *config_.maxRetransmitTime |
| 130 | : static_cast<uint16_t>(-1); |
| 131 | } |
| 132 | virtual uint16_t maxRetransmits() const { |
| 133 | return config_.maxRetransmits ? *config_.maxRetransmits |
| 134 | : static_cast<uint16_t>(-1); |
| 135 | } |
| 136 | virtual absl::optional<int> maxPacketLifeTime() const { |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 137 | return config_.maxRetransmitTime; |
| 138 | } |
Harald Alvestrand | f3736ed | 2019-04-08 11:09:30 | [diff] [blame] | 139 | virtual absl::optional<int> maxRetransmitsOpt() const { |
| 140 | return config_.maxRetransmits; |
| 141 | } |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 142 | virtual std::string protocol() const { return config_.protocol; } |
| 143 | virtual bool negotiated() const { return config_.negotiated; } |
| 144 | virtual int id() const { return config_.id; } |
Harald Alvestrand | 928e7a3 | 2019-07-31 11:16:45 | [diff] [blame] | 145 | virtual int internal_id() const { return internal_id_; } |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 146 | virtual uint64_t buffered_amount() const; |
| 147 | virtual void Close(); |
| 148 | virtual DataState state() const { return state_; } |
| 149 | virtual uint32_t messages_sent() const { return messages_sent_; } |
| 150 | virtual uint64_t bytes_sent() const { return bytes_sent_; } |
| 151 | virtual uint32_t messages_received() const { return messages_received_; } |
| 152 | virtual uint64_t bytes_received() const { return bytes_received_; } |
| 153 | virtual bool Send(const DataBuffer& buffer); |
| 154 | |
Harald Alvestrand | 1f928d3 | 2019-03-28 10:29:38 | [diff] [blame] | 155 | // Close immediately, ignoring any queued data or closing procedure. |
| 156 | // This is called for RTP data channels when SDP indicates a channel should |
| 157 | // be removed, or SCTP data channels when the underlying SctpTransport is |
| 158 | // being destroyed. |
| 159 | // It is also called by the PeerConnection if SCTP ID assignment fails. |
| 160 | void CloseAbruptly(); |
| 161 | |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 162 | // Called when the channel's ready to use. That can happen when the |
| 163 | // underlying DataMediaChannel becomes ready, or when this channel is a new |
| 164 | // stream on an existing DataMediaChannel, and we've finished negotiation. |
| 165 | void OnChannelReady(bool writable); |
| 166 | |
| 167 | // Slots for provider to connect signals to. |
| 168 | void OnDataReceived(const cricket::ReceiveDataParams& params, |
| 169 | const rtc::CopyOnWriteBuffer& payload); |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 170 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 171 | /******************************************** |
| 172 | * The following methods are for SCTP only. * |
| 173 | ********************************************/ |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 174 | |
| 175 | // Sets the SCTP sid and adds to transport layer if not set yet. Should only |
| 176 | // be called once. |
| 177 | void SetSctpSid(int sid); |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 178 | // The remote side started the closing procedure by resetting its outgoing |
| 179 | // stream (our incoming stream). Sets state to kClosing. |
| 180 | void OnClosingProcedureStartedRemotely(int sid); |
| 181 | // The closing procedure is complete; both incoming and outgoing stream |
| 182 | // resets are done and the channel can transition to kClosed. Called |
| 183 | // asynchronously after RemoveSctpDataStream. |
| 184 | void OnClosingProcedureComplete(int sid); |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 185 | // Called when the transport channel is created. |
| 186 | // Only needs to be called for SCTP data channels. |
| 187 | void OnTransportChannelCreated(); |
| 188 | // Called when the transport channel is destroyed. |
| 189 | // This method makes sure the DataChannel is disconnected and changes state |
| 190 | // to kClosed. |
| 191 | void OnTransportChannelDestroyed(); |
| 192 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 193 | /******************************************* |
| 194 | * The following methods are for RTP only. * |
| 195 | *******************************************/ |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 196 | |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 197 | // The remote peer requested that this channel should be closed. |
| 198 | void RemotePeerRequestClose(); |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 199 | // Set the SSRC this channel should use to send data on the |
| 200 | // underlying data engine. |send_ssrc| == 0 means that the channel is no |
| 201 | // longer part of the session negotiation. |
| 202 | void SetSendSsrc(uint32_t send_ssrc); |
| 203 | // Set the SSRC this channel should use to receive data from the |
| 204 | // underlying data engine. |
| 205 | void SetReceiveSsrc(uint32_t receive_ssrc); |
| 206 | |
| 207 | cricket::DataChannelType data_channel_type() const { |
| 208 | return data_channel_type_; |
| 209 | } |
| 210 | |
| 211 | // Emitted when state transitions to kOpen. |
| 212 | sigslot::signal1<DataChannel*> SignalOpened; |
| 213 | // Emitted when state transitions to kClosed. |
| 214 | // In the case of SCTP channels, this signal can be used to tell when the |
| 215 | // channel's sid is free. |
| 216 | sigslot::signal1<DataChannel*> SignalClosed; |
| 217 | |
Harald Alvestrand | 928e7a3 | 2019-07-31 11:16:45 | [diff] [blame] | 218 | // Reset the allocator for internal ID values for testing, so that |
| 219 | // the internal IDs generated are predictable. Test only. |
| 220 | static void ResetInternalIdAllocatorForTesting(int new_value); |
| 221 | |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 222 | protected: |
| 223 | DataChannel(DataChannelProviderInterface* client, |
| 224 | cricket::DataChannelType dct, |
| 225 | const std::string& label); |
| 226 | virtual ~DataChannel(); |
| 227 | |
| 228 | private: |
| 229 | // A packet queue which tracks the total queued bytes. Queued packets are |
| 230 | // owned by this class. |
Steve Anton | 944c755 | 2018-12-13 22:19:10 | [diff] [blame] | 231 | class PacketQueue final { |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 232 | public: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 233 | size_t byte_count() const { return byte_count_; } |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 234 | |
| 235 | bool Empty() const; |
| 236 | |
Steve Anton | 944c755 | 2018-12-13 22:19:10 | [diff] [blame] | 237 | std::unique_ptr<DataBuffer> PopFront(); |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 238 | |
Steve Anton | 944c755 | 2018-12-13 22:19:10 | [diff] [blame] | 239 | void PushFront(std::unique_ptr<DataBuffer> packet); |
| 240 | void PushBack(std::unique_ptr<DataBuffer> packet); |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 241 | |
| 242 | void Clear(); |
| 243 | |
| 244 | void Swap(PacketQueue* other); |
| 245 | |
| 246 | private: |
Steve Anton | 944c755 | 2018-12-13 22:19:10 | [diff] [blame] | 247 | std::deque<std::unique_ptr<DataBuffer>> packets_; |
| 248 | size_t byte_count_ = 0; |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | // The OPEN(_ACK) signaling state. |
| 252 | enum HandshakeState { |
| 253 | kHandshakeInit, |
| 254 | kHandshakeShouldSendOpen, |
| 255 | kHandshakeShouldSendAck, |
| 256 | kHandshakeWaitingForAck, |
| 257 | kHandshakeReady |
| 258 | }; |
| 259 | |
| 260 | bool Init(const InternalDataChannelInit& config); |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 261 | void UpdateState(); |
| 262 | void SetState(DataState state); |
| 263 | void DisconnectFromProvider(); |
| 264 | |
| 265 | void DeliverQueuedReceivedData(); |
| 266 | |
| 267 | void SendQueuedDataMessages(); |
| 268 | bool SendDataMessage(const DataBuffer& buffer, bool queue_if_blocked); |
| 269 | bool QueueSendDataMessage(const DataBuffer& buffer); |
| 270 | |
| 271 | void SendQueuedControlMessages(); |
| 272 | void QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer); |
| 273 | bool SendControlMessage(const rtc::CopyOnWriteBuffer& buffer); |
| 274 | |
Harald Alvestrand | 928e7a3 | 2019-07-31 11:16:45 | [diff] [blame] | 275 | const int internal_id_; |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 276 | std::string label_; |
| 277 | InternalDataChannelInit config_; |
| 278 | DataChannelObserver* observer_; |
| 279 | DataState state_; |
| 280 | uint32_t messages_sent_; |
| 281 | uint64_t bytes_sent_; |
| 282 | uint32_t messages_received_; |
| 283 | uint64_t bytes_received_; |
Marina Ciocea | e448a3f | 2019-03-04 14:52:21 | [diff] [blame] | 284 | // Number of bytes of data that have been queued using Send(). Increased |
| 285 | // before each transport send and decreased after each successful send. |
| 286 | uint64_t buffered_amount_; |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 287 | cricket::DataChannelType data_channel_type_; |
| 288 | DataChannelProviderInterface* provider_; |
| 289 | HandshakeState handshake_state_; |
| 290 | bool connected_to_provider_; |
| 291 | bool send_ssrc_set_; |
| 292 | bool receive_ssrc_set_; |
| 293 | bool writable_; |
Taylor Brandstetter | cdd05f0 | 2018-05-31 20:23:32 | [diff] [blame] | 294 | // Did we already start the graceful SCTP closing procedure? |
| 295 | bool started_closing_procedure_ = false; |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 296 | uint32_t send_ssrc_; |
| 297 | uint32_t receive_ssrc_; |
| 298 | // Control messages that always have to get sent out before any queued |
| 299 | // data. |
| 300 | PacketQueue queued_control_data_; |
| 301 | PacketQueue queued_received_data_; |
| 302 | PacketQueue queued_send_data_; |
Steve Anton | 044a04d | 2018-08-31 20:51:19 | [diff] [blame] | 303 | rtc::AsyncInvoker invoker_; |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | // Define proxy for DataChannelInterface. |
| 307 | BEGIN_SIGNALING_PROXY_MAP(DataChannel) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 308 | PROXY_SIGNALING_THREAD_DESTRUCTOR() |
| 309 | PROXY_METHOD1(void, RegisterObserver, DataChannelObserver*) |
| 310 | PROXY_METHOD0(void, UnregisterObserver) |
| 311 | PROXY_CONSTMETHOD0(std::string, label) |
| 312 | PROXY_CONSTMETHOD0(bool, reliable) |
| 313 | PROXY_CONSTMETHOD0(bool, ordered) |
| 314 | PROXY_CONSTMETHOD0(uint16_t, maxRetransmitTime) |
| 315 | PROXY_CONSTMETHOD0(uint16_t, maxRetransmits) |
Harald Alvestrand | f3736ed | 2019-04-08 11:09:30 | [diff] [blame] | 316 | PROXY_CONSTMETHOD0(absl::optional<int>, maxRetransmitsOpt) |
| 317 | PROXY_CONSTMETHOD0(absl::optional<int>, maxPacketLifeTime) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 318 | PROXY_CONSTMETHOD0(std::string, protocol) |
| 319 | PROXY_CONSTMETHOD0(bool, negotiated) |
| 320 | PROXY_CONSTMETHOD0(int, id) |
| 321 | PROXY_CONSTMETHOD0(DataState, state) |
| 322 | PROXY_CONSTMETHOD0(uint32_t, messages_sent) |
| 323 | PROXY_CONSTMETHOD0(uint64_t, bytes_sent) |
| 324 | PROXY_CONSTMETHOD0(uint32_t, messages_received) |
| 325 | PROXY_CONSTMETHOD0(uint64_t, bytes_received) |
| 326 | PROXY_CONSTMETHOD0(uint64_t, buffered_amount) |
| 327 | PROXY_METHOD0(void, Close) |
| 328 | PROXY_METHOD1(bool, Send, const DataBuffer&) |
ossu | 7bb87ee | 2017-01-23 12:56:25 | [diff] [blame] | 329 | END_PROXY_MAP() |
| 330 | |
| 331 | } // namespace webrtc |
| 332 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 333 | #endif // PC_DATA_CHANNEL_H_ |