blob: e08830feaf394c7f20fbd82c0a1e8cf3c4546187 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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:36 +00009 */
10
11// This file contains interfaces for DataChannels
12// http://dev.w3.org/2011/webrtc/editor/webrtc.html#rtcdatachannel
13
Steve Anton10542f22019-01-11 09:11:00 -080014#ifndef API_DATA_CHANNEL_INTERFACE_H_
15#define API_DATA_CHANNEL_INTERFACE_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
Yves Gerey988cc082018-10-23 12:03:01 +020017#include <stddef.h>
18#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020019
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020#include <string>
21
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020022#include "absl/types/optional.h"
Harald Alvestranddfbfb462019-12-08 05:55:43 +010023#include "api/rtc_error.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/copy_on_write_buffer.h"
26#include "rtc_base/ref_count.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020027#include "rtc_base/system/rtc_export.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
29namespace webrtc {
30
deadbeefb10f32f2017-02-08 01:38:21 -080031// C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelinit
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020032// TODO(deadbeef): Use absl::optional for the "-1 if unset" things.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033struct DataChannelInit {
deadbeefb10f32f2017-02-08 01:38:21 -080034 // Deprecated. Reliability is assumed, and channel will be unreliable if
35 // maxRetransmitTime or MaxRetransmits is set.
36 bool reliable = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037
deadbeefb10f32f2017-02-08 01:38:21 -080038 // True if ordered delivery is required.
39 bool ordered = true;
40
41 // The max period of time in milliseconds in which retransmissions will be
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020042 // sent. After this time, no more retransmissions will be sent.
deadbeefb10f32f2017-02-08 01:38:21 -080043 //
44 // Cannot be set along with |maxRetransmits|.
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020045 // This is called |maxPacketLifeTime| in the WebRTC JS API.
46 absl::optional<int> maxRetransmitTime;
deadbeefb10f32f2017-02-08 01:38:21 -080047
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020048 // The max number of retransmissions.
deadbeefb10f32f2017-02-08 01:38:21 -080049 //
50 // Cannot be set along with |maxRetransmitTime|.
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020051 absl::optional<int> maxRetransmits;
deadbeefb10f32f2017-02-08 01:38:21 -080052
53 // This is set by the application and opaque to the WebRTC implementation.
54 std::string protocol;
55
56 // True if the channel has been externally negotiated and we do not send an
57 // in-band signalling in the form of an "open" message. If this is true, |id|
58 // below must be set; otherwise it should be unset and will be negotiated
59 // in-band.
60 bool negotiated = false;
61
62 // The stream id, or SID, for SCTP data channels. -1 if unset (see above).
63 int id = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064};
65
deadbeefb10f32f2017-02-08 01:38:21 -080066// At the JavaScript level, data can be passed in as a string or a blob, so
67// this structure's |binary| flag tells whether the data should be interpreted
68// as binary or text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069struct DataBuffer {
jbaucheec21bd2016-03-20 06:15:43 -070070 DataBuffer(const rtc::CopyOnWriteBuffer& data, bool binary)
Yves Gerey665174f2018-06-19 15:03:05 +020071 : data(data), binary(binary) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 // For convenience for unit tests.
73 explicit DataBuffer(const std::string& text)
Yves Gerey665174f2018-06-19 15:03:05 +020074 : data(text.data(), text.length()), binary(false) {}
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +000075 size_t size() const { return data.size(); }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000076
jbaucheec21bd2016-03-20 06:15:43 -070077 rtc::CopyOnWriteBuffer data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 // Indicates if the received data contains UTF-8 or binary data.
79 // Note that the upper layers are left to verify the UTF-8 encoding.
80 // TODO(jiayl): prefer to use an enum instead of a bool.
81 bool binary;
82};
83
deadbeefb10f32f2017-02-08 01:38:21 -080084// Used to implement RTCDataChannel events.
85//
86// The code responding to these callbacks should unwind the stack before
87// using any other webrtc APIs; re-entrancy is not supported.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088class DataChannelObserver {
89 public:
90 // The data channel state have changed.
91 virtual void OnStateChange() = 0;
92 // A data buffer was successfully received.
93 virtual void OnMessage(const DataBuffer& buffer) = 0;
bemasc0edd50c2015-07-01 13:34:33 -070094 // The data channel's buffered_amount has changed.
Marina Cioceae448a3f2019-03-04 15:52:21 +010095 virtual void OnBufferedAmountChange(uint64_t sent_data_size) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096
97 protected:
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +020098 virtual ~DataChannelObserver() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099};
100
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200101class RTC_EXPORT DataChannelInterface : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 public:
deadbeefb10f32f2017-02-08 01:38:21 -0800103 // C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelstate
104 // Unlikely to change, but keep in sync with DataChannel.java:State and
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000105 // RTCDataChannel.h:RTCDataChannelState.
106 enum DataState {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 kConnecting,
108 kOpen, // The DataChannel is ready to send data.
109 kClosing,
110 kClosed
111 };
112
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000113 static const char* DataStateString(DataState state) {
114 switch (state) {
115 case kConnecting:
116 return "connecting";
117 case kOpen:
118 return "open";
119 case kClosing:
120 return "closing";
121 case kClosed:
122 return "closed";
123 }
henrikg91d6ede2015-09-17 00:24:34 -0700124 RTC_CHECK(false) << "Unknown DataChannel state: " << state;
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000125 return "";
126 }
127
deadbeefb10f32f2017-02-08 01:38:21 -0800128 // Used to receive events from the data channel. Only one observer can be
129 // registered at a time. UnregisterObserver should be called before the
130 // observer object is destroyed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 virtual void RegisterObserver(DataChannelObserver* observer) = 0;
132 virtual void UnregisterObserver() = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800133
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 // The label attribute represents a label that can be used to distinguish this
135 // DataChannel object from other DataChannel objects.
136 virtual std::string label() const = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000137
deadbeefb10f32f2017-02-08 01:38:21 -0800138 // The accessors below simply return the properties from the DataChannelInit
139 // the data channel was constructed with.
140 virtual bool reliable() const = 0;
141 // TODO(deadbeef): Remove these dummy implementations when all classes have
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000142 // implemented these APIs. They should all just return the values the
143 // DataChannel was created with.
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +0200144 virtual bool ordered() const;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200145 // TODO(hta): Deprecate and remove the following two functions.
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +0200146 virtual uint16_t maxRetransmitTime() const;
147 virtual uint16_t maxRetransmits() const;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200148 virtual absl::optional<int> maxRetransmitsOpt() const;
149 virtual absl::optional<int> maxPacketLifeTime() const;
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +0200150 virtual std::string protocol() const;
151 virtual bool negotiated() const;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000152
deadbeefb10f32f2017-02-08 01:38:21 -0800153 // Returns the ID from the DataChannelInit, if it was negotiated out-of-band.
154 // If negotiated in-band, this ID will be populated once the DTLS role is
155 // determined, and until then this will return -1.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 virtual int id() const = 0;
157 virtual DataState state() const = 0;
Harald Alvestranddfbfb462019-12-08 05:55:43 +0100158 // When state is kClosed, and the DataChannel was not closed using
159 // the closing procedure, returns the error information about the closing.
160 // The default implementation returns "no error".
161 virtual RTCError error() const { return RTCError(); }
hbosc42d3762016-11-24 01:17:52 -0800162 virtual uint32_t messages_sent() const = 0;
163 virtual uint64_t bytes_sent() const = 0;
164 virtual uint32_t messages_received() const = 0;
165 virtual uint64_t bytes_received() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800166
167 // Returns the number of bytes of application data (UTF-8 text and binary
168 // data) that have been queued using Send but have not yet been processed at
169 // the SCTP level. See comment above Send below.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200170 virtual uint64_t buffered_amount() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800171
172 // Begins the graceful data channel closing procedure. See:
173 // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.7
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 virtual void Close() = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800175
176 // Sends |data| to the remote peer. If the data can't be sent at the SCTP
177 // level (due to congestion control), it's buffered at the data channel level,
178 // up to a maximum of 16MB. If Send is called while this buffer is full, the
179 // data channel will be closed abruptly.
180 //
181 // So, it's important to use buffered_amount() and OnBufferedAmountChange to
182 // ensure the data channel is used efficiently but without filling this
183 // buffer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 virtual bool Send(const DataBuffer& buffer) = 0;
185
186 protected:
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +0200187 ~DataChannelInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188};
189
190} // namespace webrtc
191
Steve Anton10542f22019-01-11 09:11:00 -0800192#endif // API_DATA_CHANNEL_INTERFACE_H_