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