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