blob: f7032ec0695d3e7029edf6b03881942102824602 [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>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019#include <string>
20
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020021#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/copy_on_write_buffer.h"
24#include "rtc_base/ref_count.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
26namespace webrtc {
27
deadbeefb10f32f2017-02-08 01:38:21 -080028// C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelinit
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020029// TODO(deadbeef): Use absl::optional for the "-1 if unset" things.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030struct DataChannelInit {
deadbeefb10f32f2017-02-08 01:38:21 -080031 // Deprecated. Reliability is assumed, and channel will be unreliable if
32 // maxRetransmitTime or MaxRetransmits is set.
33 bool reliable = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
deadbeefb10f32f2017-02-08 01:38:21 -080035 // True if ordered delivery is required.
36 bool ordered = true;
37
38 // The max period of time in milliseconds in which retransmissions will be
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020039 // sent. After this time, no more retransmissions will be sent.
deadbeefb10f32f2017-02-08 01:38:21 -080040 //
41 // Cannot be set along with |maxRetransmits|.
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020042 // This is called |maxPacketLifeTime| in the WebRTC JS API.
43 absl::optional<int> maxRetransmitTime;
deadbeefb10f32f2017-02-08 01:38:21 -080044
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020045 // The max number of retransmissions.
deadbeefb10f32f2017-02-08 01:38:21 -080046 //
47 // Cannot be set along with |maxRetransmitTime|.
Harald Alvestrandf3736ed2019-04-08 13:09:30 +020048 absl::optional<int> maxRetransmits;
deadbeefb10f32f2017-02-08 01:38:21 -080049
50 // This is set by the application and opaque to the WebRTC implementation.
51 std::string protocol;
52
53 // True if the channel has been externally negotiated and we do not send an
54 // in-band signalling in the form of an "open" message. If this is true, |id|
55 // below must be set; otherwise it should be unset and will be negotiated
56 // in-band.
57 bool negotiated = false;
58
59 // The stream id, or SID, for SCTP data channels. -1 if unset (see above).
60 int id = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061};
62
deadbeefb10f32f2017-02-08 01:38:21 -080063// At the JavaScript level, data can be passed in as a string or a blob, so
64// this structure's |binary| flag tells whether the data should be interpreted
65// as binary or text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066struct DataBuffer {
jbaucheec21bd2016-03-20 06:15:43 -070067 DataBuffer(const rtc::CopyOnWriteBuffer& data, bool binary)
Yves Gerey665174f2018-06-19 15:03:05 +020068 : data(data), binary(binary) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 // For convenience for unit tests.
70 explicit DataBuffer(const std::string& text)
Yves Gerey665174f2018-06-19 15:03:05 +020071 : data(text.data(), text.length()), binary(false) {}
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +000072 size_t size() const { return data.size(); }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000073
jbaucheec21bd2016-03-20 06:15:43 -070074 rtc::CopyOnWriteBuffer data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 // Indicates if the received data contains UTF-8 or binary data.
76 // Note that the upper layers are left to verify the UTF-8 encoding.
77 // TODO(jiayl): prefer to use an enum instead of a bool.
78 bool binary;
79};
80
deadbeefb10f32f2017-02-08 01:38:21 -080081// Used to implement RTCDataChannel events.
82//
83// The code responding to these callbacks should unwind the stack before
84// using any other webrtc APIs; re-entrancy is not supported.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085class DataChannelObserver {
86 public:
87 // The data channel state have changed.
88 virtual void OnStateChange() = 0;
89 // A data buffer was successfully received.
90 virtual void OnMessage(const DataBuffer& buffer) = 0;
bemasc0edd50c2015-07-01 13:34:33 -070091 // The data channel's buffered_amount has changed.
Marina Cioceae448a3f2019-03-04 15:52:21 +010092 virtual void OnBufferedAmountChange(uint64_t sent_data_size) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093
94 protected:
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +020095 virtual ~DataChannelObserver() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096};
97
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000098class DataChannelInterface : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 public:
deadbeefb10f32f2017-02-08 01:38:21 -0800100 // C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelstate
101 // Unlikely to change, but keep in sync with DataChannel.java:State and
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000102 // RTCDataChannel.h:RTCDataChannelState.
103 enum DataState {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 kConnecting,
105 kOpen, // The DataChannel is ready to send data.
106 kClosing,
107 kClosed
108 };
109
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000110 static const char* DataStateString(DataState state) {
111 switch (state) {
112 case kConnecting:
113 return "connecting";
114 case kOpen:
115 return "open";
116 case kClosing:
117 return "closing";
118 case kClosed:
119 return "closed";
120 }
henrikg91d6ede2015-09-17 00:24:34 -0700121 RTC_CHECK(false) << "Unknown DataChannel state: " << state;
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000122 return "";
123 }
124
deadbeefb10f32f2017-02-08 01:38:21 -0800125 // Used to receive events from the data channel. Only one observer can be
126 // registered at a time. UnregisterObserver should be called before the
127 // observer object is destroyed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 virtual void RegisterObserver(DataChannelObserver* observer) = 0;
129 virtual void UnregisterObserver() = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800130
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 // The label attribute represents a label that can be used to distinguish this
132 // DataChannel object from other DataChannel objects.
133 virtual std::string label() const = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000134
deadbeefb10f32f2017-02-08 01:38:21 -0800135 // The accessors below simply return the properties from the DataChannelInit
136 // the data channel was constructed with.
137 virtual bool reliable() const = 0;
138 // TODO(deadbeef): Remove these dummy implementations when all classes have
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000139 // implemented these APIs. They should all just return the values the
140 // DataChannel was created with.
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +0200141 virtual bool ordered() const;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200142 // TODO(hta): Deprecate and remove the following two functions.
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +0200143 virtual uint16_t maxRetransmitTime() const;
144 virtual uint16_t maxRetransmits() const;
Harald Alvestrandf3736ed2019-04-08 13:09:30 +0200145 virtual absl::optional<int> maxRetransmitsOpt() const;
146 virtual absl::optional<int> maxPacketLifeTime() const;
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +0200147 virtual std::string protocol() const;
148 virtual bool negotiated() const;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000149
deadbeefb10f32f2017-02-08 01:38:21 -0800150 // Returns the ID from the DataChannelInit, if it was negotiated out-of-band.
151 // If negotiated in-band, this ID will be populated once the DTLS role is
152 // determined, and until then this will return -1.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 virtual int id() const = 0;
154 virtual DataState state() const = 0;
hbosc42d3762016-11-24 01:17:52 -0800155 virtual uint32_t messages_sent() const = 0;
156 virtual uint64_t bytes_sent() const = 0;
157 virtual uint32_t messages_received() const = 0;
158 virtual uint64_t bytes_received() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800159
160 // Returns the number of bytes of application data (UTF-8 text and binary
161 // data) that have been queued using Send but have not yet been processed at
162 // the SCTP level. See comment above Send below.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200163 virtual uint64_t buffered_amount() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800164
165 // Begins the graceful data channel closing procedure. See:
166 // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.7
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 virtual void Close() = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800168
169 // Sends |data| to the remote peer. If the data can't be sent at the SCTP
170 // level (due to congestion control), it's buffered at the data channel level,
171 // up to a maximum of 16MB. If Send is called while this buffer is full, the
172 // data channel will be closed abruptly.
173 //
174 // So, it's important to use buffered_amount() and OnBufferedAmountChange to
175 // ensure the data channel is used efficiently but without filling this
176 // buffer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 virtual bool Send(const DataBuffer& buffer) = 0;
178
179 protected:
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +0200180 ~DataChannelInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181};
182
183} // namespace webrtc
184
Steve Anton10542f22019-01-11 09:11:00 -0800185#endif // API_DATA_CHANNEL_INTERFACE_H_