henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | // This file contains interfaces for DataChannels |
| 12 | // http://dev.w3.org/2011/webrtc/editor/webrtc.html#rtcdatachannel |
| 13 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 14 | #ifndef API_DATA_CHANNEL_INTERFACE_H_ |
| 15 | #define API_DATA_CHANNEL_INTERFACE_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 16 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 17 | #include <stddef.h> |
| 18 | #include <stdint.h> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "rtc_base/copy_on_write_buffer.h" |
| 23 | #include "rtc_base/ref_count.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 27 | // C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelinit |
Danil Chapovalov | 0bc58cf | 2018-06-21 13:32:56 +0200 | [diff] [blame] | 28 | // TODO(deadbeef): Use absl::optional for the "-1 if unset" things. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | struct DataChannelInit { |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 30 | // Deprecated. Reliability is assumed, and channel will be unreliable if |
| 31 | // maxRetransmitTime or MaxRetransmits is set. |
| 32 | bool reliable = false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 34 | // True if ordered delivery is required. |
| 35 | bool ordered = true; |
| 36 | |
| 37 | // The max period of time in milliseconds in which retransmissions will be |
| 38 | // sent. After this time, no more retransmissions will be sent. -1 if unset. |
| 39 | // |
| 40 | // Cannot be set along with |maxRetransmits|. |
| 41 | int maxRetransmitTime = -1; |
| 42 | |
| 43 | // The max number of retransmissions. -1 if unset. |
| 44 | // |
| 45 | // Cannot be set along with |maxRetransmitTime|. |
| 46 | int maxRetransmits = -1; |
| 47 | |
| 48 | // This is set by the application and opaque to the WebRTC implementation. |
| 49 | std::string protocol; |
| 50 | |
| 51 | // True if the channel has been externally negotiated and we do not send an |
| 52 | // in-band signalling in the form of an "open" message. If this is true, |id| |
| 53 | // below must be set; otherwise it should be unset and will be negotiated |
| 54 | // in-band. |
| 55 | bool negotiated = false; |
| 56 | |
| 57 | // The stream id, or SID, for SCTP data channels. -1 if unset (see above). |
| 58 | int id = -1; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 61 | // At the JavaScript level, data can be passed in as a string or a blob, so |
| 62 | // this structure's |binary| flag tells whether the data should be interpreted |
| 63 | // as binary or text. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 64 | struct DataBuffer { |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 65 | DataBuffer(const rtc::CopyOnWriteBuffer& data, bool binary) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 66 | : data(data), binary(binary) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 67 | // For convenience for unit tests. |
| 68 | explicit DataBuffer(const std::string& text) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 69 | : data(text.data(), text.length()), binary(false) {} |
kwiberg@webrtc.org | eebcab5 | 2015-03-24 09:19:06 +0000 | [diff] [blame] | 70 | size_t size() const { return data.size(); } |
wu@webrtc.org | d64719d | 2013-08-01 00:00:07 +0000 | [diff] [blame] | 71 | |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 72 | rtc::CopyOnWriteBuffer data; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | // Indicates if the received data contains UTF-8 or binary data. |
| 74 | // Note that the upper layers are left to verify the UTF-8 encoding. |
| 75 | // TODO(jiayl): prefer to use an enum instead of a bool. |
| 76 | bool binary; |
| 77 | }; |
| 78 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 79 | // Used to implement RTCDataChannel events. |
| 80 | // |
| 81 | // The code responding to these callbacks should unwind the stack before |
| 82 | // using any other webrtc APIs; re-entrancy is not supported. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | class DataChannelObserver { |
| 84 | public: |
| 85 | // The data channel state have changed. |
| 86 | virtual void OnStateChange() = 0; |
| 87 | // A data buffer was successfully received. |
| 88 | virtual void OnMessage(const DataBuffer& buffer) = 0; |
bemasc | 0edd50c | 2015-07-01 13:34:33 -0700 | [diff] [blame] | 89 | // The data channel's buffered_amount has changed. |
Marina Ciocea | e448a3f | 2019-03-04 15:52:21 +0100 | [diff] [blame] | 90 | virtual void OnBufferedAmountChange(uint64_t sent_data_size) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | |
| 92 | protected: |
Mirko Bonadei | 79eb4dd | 2018-07-19 10:39:30 +0200 | [diff] [blame] | 93 | virtual ~DataChannelObserver() = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 96 | class DataChannelInterface : public rtc::RefCountInterface { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | public: |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 98 | // C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelstate |
| 99 | // Unlikely to change, but keep in sync with DataChannel.java:State and |
tkchin@webrtc.org | ff27332 | 2014-04-30 18:32:33 +0000 | [diff] [blame] | 100 | // RTCDataChannel.h:RTCDataChannelState. |
| 101 | enum DataState { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | kConnecting, |
| 103 | kOpen, // The DataChannel is ready to send data. |
| 104 | kClosing, |
| 105 | kClosed |
| 106 | }; |
| 107 | |
decurtis@webrtc.org | 487a444 | 2015-01-15 22:55:07 +0000 | [diff] [blame] | 108 | static const char* DataStateString(DataState state) { |
| 109 | switch (state) { |
| 110 | case kConnecting: |
| 111 | return "connecting"; |
| 112 | case kOpen: |
| 113 | return "open"; |
| 114 | case kClosing: |
| 115 | return "closing"; |
| 116 | case kClosed: |
| 117 | return "closed"; |
| 118 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 119 | RTC_CHECK(false) << "Unknown DataChannel state: " << state; |
decurtis@webrtc.org | 487a444 | 2015-01-15 22:55:07 +0000 | [diff] [blame] | 120 | return ""; |
| 121 | } |
| 122 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 123 | // Used to receive events from the data channel. Only one observer can be |
| 124 | // registered at a time. UnregisterObserver should be called before the |
| 125 | // observer object is destroyed. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 126 | virtual void RegisterObserver(DataChannelObserver* observer) = 0; |
| 127 | virtual void UnregisterObserver() = 0; |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 128 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 129 | // The label attribute represents a label that can be used to distinguish this |
| 130 | // DataChannel object from other DataChannel objects. |
| 131 | virtual std::string label() const = 0; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 132 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 133 | // The accessors below simply return the properties from the DataChannelInit |
| 134 | // the data channel was constructed with. |
| 135 | virtual bool reliable() const = 0; |
| 136 | // TODO(deadbeef): Remove these dummy implementations when all classes have |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 137 | // implemented these APIs. They should all just return the values the |
| 138 | // DataChannel was created with. |
Mirko Bonadei | 79eb4dd | 2018-07-19 10:39:30 +0200 | [diff] [blame] | 139 | virtual bool ordered() const; |
| 140 | virtual uint16_t maxRetransmitTime() const; |
| 141 | virtual uint16_t maxRetransmits() const; |
| 142 | virtual std::string protocol() const; |
| 143 | virtual bool negotiated() const; |
wu@webrtc.org | 822fbd8 | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 144 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 145 | // Returns the ID from the DataChannelInit, if it was negotiated out-of-band. |
| 146 | // If negotiated in-band, this ID will be populated once the DTLS role is |
| 147 | // determined, and until then this will return -1. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 148 | virtual int id() const = 0; |
| 149 | virtual DataState state() const = 0; |
hbos | c42d376 | 2016-11-24 01:17:52 -0800 | [diff] [blame] | 150 | virtual uint32_t messages_sent() const = 0; |
| 151 | virtual uint64_t bytes_sent() const = 0; |
| 152 | virtual uint32_t messages_received() const = 0; |
| 153 | virtual uint64_t bytes_received() const = 0; |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 154 | |
| 155 | // Returns the number of bytes of application data (UTF-8 text and binary |
| 156 | // data) that have been queued using Send but have not yet been processed at |
| 157 | // the SCTP level. See comment above Send below. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 158 | virtual uint64_t buffered_amount() const = 0; |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 159 | |
| 160 | // Begins the graceful data channel closing procedure. See: |
| 161 | // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.7 |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 162 | virtual void Close() = 0; |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 163 | |
| 164 | // Sends |data| to the remote peer. If the data can't be sent at the SCTP |
| 165 | // level (due to congestion control), it's buffered at the data channel level, |
| 166 | // up to a maximum of 16MB. If Send is called while this buffer is full, the |
| 167 | // data channel will be closed abruptly. |
| 168 | // |
| 169 | // So, it's important to use buffered_amount() and OnBufferedAmountChange to |
| 170 | // ensure the data channel is used efficiently but without filling this |
| 171 | // buffer. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 172 | virtual bool Send(const DataBuffer& buffer) = 0; |
| 173 | |
| 174 | protected: |
Mirko Bonadei | 79eb4dd | 2018-07-19 10:39:30 +0200 | [diff] [blame] | 175 | ~DataChannelInterface() override = default; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | } // namespace webrtc |
| 179 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 180 | #endif // API_DATA_CHANNEL_INTERFACE_H_ |