blob: bf27c6c4f319bb97939153a4ac07d137fddc226b [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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.org28e20752013-07-10 00:45:369 */
10
11// This file contains interfaces for DataChannels
12// http://dev.w3.org/2011/webrtc/editor/webrtc.html#rtcdatachannel
13
Steve Anton10542f22019-01-11 17:11:0014#ifndef API_DATA_CHANNEL_INTERFACE_H_
15#define API_DATA_CHANNEL_INTERFACE_H_
henrike@webrtc.org28e20752013-07-10 00:45:3616
Yves Gerey988cc082018-10-23 10:03:0117#include <stddef.h>
18#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3319
henrike@webrtc.org28e20752013-07-10 00:45:3620#include <string>
21
Tommia50a81a2023-04-11 15:32:3422#include "absl/functional/any_invocable.h"
Harald Alvestrandf3736ed2019-04-08 11:09:3023#include "absl/types/optional.h"
Harald Alvestrandfd5ae7f2020-05-16 06:37:4924#include "api/priority.h"
Harald Alvestranddfbfb462019-12-08 04:55:4325#include "api/rtc_error.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3126#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0027#include "rtc_base/copy_on_write_buffer.h"
28#include "rtc_base/ref_count.h"
Mirko Bonadei35214fc2019-09-23 12:54:2829#include "rtc_base/system/rtc_export.h"
henrike@webrtc.org28e20752013-07-10 00:45:3630
31namespace webrtc {
32
deadbeefb10f32f2017-02-08 09:38:2133// C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelinit
Danil Chapovalov0bc58cf2018-06-21 11:32:5634// TODO(deadbeef): Use absl::optional for the "-1 if unset" things.
henrike@webrtc.org28e20752013-07-10 00:45:3635struct DataChannelInit {
deadbeefb10f32f2017-02-08 09:38:2136 // Deprecated. Reliability is assumed, and channel will be unreliable if
37 // maxRetransmitTime or MaxRetransmits is set.
38 bool reliable = false;
henrike@webrtc.org28e20752013-07-10 00:45:3639
deadbeefb10f32f2017-02-08 09:38:2140 // True if ordered delivery is required.
41 bool ordered = true;
42
43 // The max period of time in milliseconds in which retransmissions will be
Harald Alvestrandf3736ed2019-04-08 11:09:3044 // sent. After this time, no more retransmissions will be sent.
deadbeefb10f32f2017-02-08 09:38:2145 //
Artem Titov0e61fdd2021-07-25 19:50:1446 // Cannot be set along with `maxRetransmits`.
47 // This is called `maxPacketLifeTime` in the WebRTC JS API.
Florent Castelli5183f002021-05-07 11:52:4448 // Negative values are ignored, and positive values are clamped to [0-65535]
Harald Alvestrandf3736ed2019-04-08 11:09:3049 absl::optional<int> maxRetransmitTime;
deadbeefb10f32f2017-02-08 09:38:2150
Harald Alvestrandf3736ed2019-04-08 11:09:3051 // The max number of retransmissions.
deadbeefb10f32f2017-02-08 09:38:2152 //
Artem Titov0e61fdd2021-07-25 19:50:1453 // Cannot be set along with `maxRetransmitTime`.
Florent Castelli5183f002021-05-07 11:52:4454 // Negative values are ignored, and positive values are clamped to [0-65535]
Harald Alvestrandf3736ed2019-04-08 11:09:3055 absl::optional<int> maxRetransmits;
deadbeefb10f32f2017-02-08 09:38:2156
57 // This is set by the application and opaque to the WebRTC implementation.
58 std::string protocol;
59
60 // True if the channel has been externally negotiated and we do not send an
Artem Titov0e61fdd2021-07-25 19:50:1461 // in-band signalling in the form of an "open" message. If this is true, `id`
deadbeefb10f32f2017-02-08 09:38:2162 // below must be set; otherwise it should be unset and will be negotiated
63 // in-band.
64 bool negotiated = false;
65
66 // The stream id, or SID, for SCTP data channels. -1 if unset (see above).
67 int id = -1;
Harald Alvestrandfd5ae7f2020-05-16 06:37:4968
69 // https://w3c.github.io/webrtc-priority/#new-rtcdatachannelinit-member
70 absl::optional<Priority> priority;
henrike@webrtc.org28e20752013-07-10 00:45:3671};
72
deadbeefb10f32f2017-02-08 09:38:2173// At the JavaScript level, data can be passed in as a string or a blob, so
Artem Titov0e61fdd2021-07-25 19:50:1474// this structure's `binary` flag tells whether the data should be interpreted
deadbeefb10f32f2017-02-08 09:38:2175// as binary or text.
henrike@webrtc.org28e20752013-07-10 00:45:3676struct DataBuffer {
jbaucheec21bd2016-03-20 13:15:4377 DataBuffer(const rtc::CopyOnWriteBuffer& data, bool binary)
Yves Gerey665174f2018-06-19 13:03:0578 : data(data), binary(binary) {}
henrike@webrtc.org28e20752013-07-10 00:45:3679 // For convenience for unit tests.
80 explicit DataBuffer(const std::string& text)
Yves Gerey665174f2018-06-19 13:03:0581 : data(text.data(), text.length()), binary(false) {}
kwiberg@webrtc.orgeebcab52015-03-24 09:19:0682 size_t size() const { return data.size(); }
wu@webrtc.orgd64719d2013-08-01 00:00:0783
jbaucheec21bd2016-03-20 13:15:4384 rtc::CopyOnWriteBuffer data;
henrike@webrtc.org28e20752013-07-10 00:45:3685 // Indicates if the received data contains UTF-8 or binary data.
86 // Note that the upper layers are left to verify the UTF-8 encoding.
87 // TODO(jiayl): prefer to use an enum instead of a bool.
88 bool binary;
89};
90
deadbeefb10f32f2017-02-08 09:38:2191// Used to implement RTCDataChannel events.
92//
93// The code responding to these callbacks should unwind the stack before
94// using any other webrtc APIs; re-entrancy is not supported.
henrike@webrtc.org28e20752013-07-10 00:45:3695class DataChannelObserver {
96 public:
97 // The data channel state have changed.
98 virtual void OnStateChange() = 0;
99 // A data buffer was successfully received.
100 virtual void OnMessage(const DataBuffer& buffer) = 0;
bemasc0edd50c2015-07-01 20:34:33101 // The data channel's buffered_amount has changed.
Marina Cioceae448a3f2019-03-04 14:52:21102 virtual void OnBufferedAmountChange(uint64_t sent_data_size) {}
henrike@webrtc.org28e20752013-07-10 00:45:36103
Tommia13b4d12023-04-05 15:17:17104 // Override this to get callbacks directly on the network thread.
105 // An implementation that does that must not block the network thread
106 // but rather only use the callback to trigger asynchronous processing
107 // elsewhere as a result of the notification.
108 // The default return value, `false`, means that notifications will be
109 // delivered on the signaling thread associated with the peerconnection
110 // instance.
111 // TODO(webrtc:11547): Eventually all DataChannelObserver implementations
112 // should be called on the network thread and this method removed.
113 virtual bool IsOkToCallOnTheNetworkThread() { return false; }
114
henrike@webrtc.org28e20752013-07-10 00:45:36115 protected:
Mirko Bonadei79eb4dd2018-07-19 08:39:30116 virtual ~DataChannelObserver() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36117};
118
Mirko Bonadei35214fc2019-09-23 12:54:28119class RTC_EXPORT DataChannelInterface : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36120 public:
deadbeefb10f32f2017-02-08 09:38:21121 // C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelstate
122 // Unlikely to change, but keep in sync with DataChannel.java:State and
tkchin@webrtc.orgff273322014-04-30 18:32:33123 // RTCDataChannel.h:RTCDataChannelState.
124 enum DataState {
henrike@webrtc.org28e20752013-07-10 00:45:36125 kConnecting,
126 kOpen, // The DataChannel is ready to send data.
127 kClosing,
128 kClosed
129 };
130
decurtis@webrtc.org487a4442015-01-15 22:55:07131 static const char* DataStateString(DataState state) {
132 switch (state) {
133 case kConnecting:
134 return "connecting";
135 case kOpen:
136 return "open";
137 case kClosing:
138 return "closing";
139 case kClosed:
140 return "closed";
141 }
henrikg91d6ede2015-09-17 07:24:34142 RTC_CHECK(false) << "Unknown DataChannel state: " << state;
decurtis@webrtc.org487a4442015-01-15 22:55:07143 return "";
144 }
145
deadbeefb10f32f2017-02-08 09:38:21146 // Used to receive events from the data channel. Only one observer can be
147 // registered at a time. UnregisterObserver should be called before the
148 // observer object is destroyed.
henrike@webrtc.org28e20752013-07-10 00:45:36149 virtual void RegisterObserver(DataChannelObserver* observer) = 0;
150 virtual void UnregisterObserver() = 0;
deadbeefb10f32f2017-02-08 09:38:21151
henrike@webrtc.org28e20752013-07-10 00:45:36152 // The label attribute represents a label that can be used to distinguish this
153 // DataChannel object from other DataChannel objects.
154 virtual std::string label() const = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54155
deadbeefb10f32f2017-02-08 09:38:21156 // The accessors below simply return the properties from the DataChannelInit
157 // the data channel was constructed with.
158 virtual bool reliable() const = 0;
159 // TODO(deadbeef): Remove these dummy implementations when all classes have
wu@webrtc.org822fbd82013-08-15 23:38:54160 // implemented these APIs. They should all just return the values the
161 // DataChannel was created with.
Mirko Bonadei79eb4dd2018-07-19 08:39:30162 virtual bool ordered() const;
Harald Alvestrandf3736ed2019-04-08 11:09:30163 // TODO(hta): Deprecate and remove the following two functions.
Mirko Bonadei79eb4dd2018-07-19 08:39:30164 virtual uint16_t maxRetransmitTime() const;
165 virtual uint16_t maxRetransmits() const;
Harald Alvestrandf3736ed2019-04-08 11:09:30166 virtual absl::optional<int> maxRetransmitsOpt() const;
167 virtual absl::optional<int> maxPacketLifeTime() const;
Mirko Bonadei79eb4dd2018-07-19 08:39:30168 virtual std::string protocol() const;
169 virtual bool negotiated() const;
wu@webrtc.org822fbd82013-08-15 23:38:54170
deadbeefb10f32f2017-02-08 09:38:21171 // Returns the ID from the DataChannelInit, if it was negotiated out-of-band.
172 // If negotiated in-band, this ID will be populated once the DTLS role is
173 // determined, and until then this will return -1.
henrike@webrtc.org28e20752013-07-10 00:45:36174 virtual int id() const = 0;
Harald Alvestrandfd5ae7f2020-05-16 06:37:49175 virtual Priority priority() const { return Priority::kLow; }
henrike@webrtc.org28e20752013-07-10 00:45:36176 virtual DataState state() const = 0;
Harald Alvestranddfbfb462019-12-08 04:55:43177 // When state is kClosed, and the DataChannel was not closed using
178 // the closing procedure, returns the error information about the closing.
179 // The default implementation returns "no error".
180 virtual RTCError error() const { return RTCError(); }
hbosc42d3762016-11-24 09:17:52181 virtual uint32_t messages_sent() const = 0;
182 virtual uint64_t bytes_sent() const = 0;
183 virtual uint32_t messages_received() const = 0;
184 virtual uint64_t bytes_received() const = 0;
deadbeefb10f32f2017-02-08 09:38:21185
186 // Returns the number of bytes of application data (UTF-8 text and binary
187 // data) that have been queued using Send but have not yet been processed at
188 // the SCTP level. See comment above Send below.
Florent Castellia563a2a2021-10-18 09:46:21189 // Values are less or equal to MaxSendQueueSize().
Peter Boström0c4e06b2015-10-07 10:23:21190 virtual uint64_t buffered_amount() const = 0;
deadbeefb10f32f2017-02-08 09:38:21191
192 // Begins the graceful data channel closing procedure. See:
193 // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.7
henrike@webrtc.org28e20752013-07-10 00:45:36194 virtual void Close() = 0;
deadbeefb10f32f2017-02-08 09:38:21195
Artem Titov0e61fdd2021-07-25 19:50:14196 // Sends `data` to the remote peer. If the data can't be sent at the SCTP
deadbeefb10f32f2017-02-08 09:38:21197 // level (due to congestion control), it's buffered at the data channel level,
Florent Castelli01343032021-11-03 15:09:46198 // up to a maximum of MaxSendQueueSize().
199 // Returns false if the data channel is not in open state or if the send
200 // buffer is full.
201 // TODO(webrtc:13289): Return an RTCError with information about the failure.
Tommia50a81a2023-04-11 15:32:34202 // TODO(tommi): Remove this method once downstream implementations don't refer
203 // to it.
204 virtual bool Send(const DataBuffer& buffer);
205
206 // Queues up an asynchronus send operation to run on a network thread.
207 // Once the operation has completed the `on_complete` callback is invoked,
208 // on the thread the send operation was done on. It's important that
209 // `on_complete` implementations do not block the current thread but rather
210 // post any expensive operations to other worker threads.
211 // TODO(tommi): Make pure virtual after updating mock class in Chromium.
212 // Deprecate `Send` in favor of this variant since the return value of `Send`
213 // is limiting for a fully async implementation (yet in practice is ignored).
214 virtual void SendAsync(DataBuffer buffer,
215 absl::AnyInvocable<void(RTCError) &&> on_complete);
henrike@webrtc.org28e20752013-07-10 00:45:36216
Florent Castellia563a2a2021-10-18 09:46:21217 // Amount of bytes that can be queued for sending on the data channel.
218 // Those are bytes that have not yet been processed at the SCTP level.
219 static uint64_t MaxSendQueueSize();
220
henrike@webrtc.org28e20752013-07-10 00:45:36221 protected:
Mirko Bonadei79eb4dd2018-07-19 08:39:30222 ~DataChannelInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36223};
224
225} // namespace webrtc
226
Steve Anton10542f22019-01-11 17:11:00227#endif // API_DATA_CHANNEL_INTERFACE_H_