blob: ccf3ad7122b8de7438a5d5e01c748627fa25bfd0 [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
Harald Alvestrandf3736ed2019-04-08 11:09:3022#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0024#include "rtc_base/copy_on_write_buffer.h"
25#include "rtc_base/ref_count.h"
Mirko Bonadei35214fc2019-09-23 12:54:2826#include "rtc_base/system/rtc_export.h"
henrike@webrtc.org28e20752013-07-10 00:45:3627
28namespace webrtc {
29
deadbeefb10f32f2017-02-08 09:38:2130// C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelinit
Danil Chapovalov0bc58cf2018-06-21 11:32:5631// TODO(deadbeef): Use absl::optional for the "-1 if unset" things.
henrike@webrtc.org28e20752013-07-10 00:45:3632struct DataChannelInit {
deadbeefb10f32f2017-02-08 09:38:2133 // Deprecated. Reliability is assumed, and channel will be unreliable if
34 // maxRetransmitTime or MaxRetransmits is set.
35 bool reliable = false;
henrike@webrtc.org28e20752013-07-10 00:45:3636
deadbeefb10f32f2017-02-08 09:38:2137 // True if ordered delivery is required.
38 bool ordered = true;
39
40 // The max period of time in milliseconds in which retransmissions will be
Harald Alvestrandf3736ed2019-04-08 11:09:3041 // sent. After this time, no more retransmissions will be sent.
deadbeefb10f32f2017-02-08 09:38:2142 //
43 // Cannot be set along with |maxRetransmits|.
Harald Alvestrandf3736ed2019-04-08 11:09:3044 // This is called |maxPacketLifeTime| in the WebRTC JS API.
45 absl::optional<int> maxRetransmitTime;
deadbeefb10f32f2017-02-08 09:38:2146
Harald Alvestrandf3736ed2019-04-08 11:09:3047 // The max number of retransmissions.
deadbeefb10f32f2017-02-08 09:38:2148 //
49 // Cannot be set along with |maxRetransmitTime|.
Harald Alvestrandf3736ed2019-04-08 11:09:3050 absl::optional<int> maxRetransmits;
deadbeefb10f32f2017-02-08 09:38:2151
52 // This is set by the application and opaque to the WebRTC implementation.
53 std::string protocol;
54
55 // True if the channel has been externally negotiated and we do not send an
56 // in-band signalling in the form of an "open" message. If this is true, |id|
57 // below must be set; otherwise it should be unset and will be negotiated
58 // in-band.
59 bool negotiated = false;
60
61 // The stream id, or SID, for SCTP data channels. -1 if unset (see above).
62 int id = -1;
henrike@webrtc.org28e20752013-07-10 00:45:3663};
64
deadbeefb10f32f2017-02-08 09:38:2165// At the JavaScript level, data can be passed in as a string or a blob, so
66// this structure's |binary| flag tells whether the data should be interpreted
67// as binary or text.
henrike@webrtc.org28e20752013-07-10 00:45:3668struct DataBuffer {
jbaucheec21bd2016-03-20 13:15:4369 DataBuffer(const rtc::CopyOnWriteBuffer& data, bool binary)
Yves Gerey665174f2018-06-19 13:03:0570 : data(data), binary(binary) {}
henrike@webrtc.org28e20752013-07-10 00:45:3671 // For convenience for unit tests.
72 explicit DataBuffer(const std::string& text)
Yves Gerey665174f2018-06-19 13:03:0573 : data(text.data(), text.length()), binary(false) {}
kwiberg@webrtc.orgeebcab52015-03-24 09:19:0674 size_t size() const { return data.size(); }
wu@webrtc.orgd64719d2013-08-01 00:00:0775
jbaucheec21bd2016-03-20 13:15:4376 rtc::CopyOnWriteBuffer data;
henrike@webrtc.org28e20752013-07-10 00:45:3677 // Indicates if the received data contains UTF-8 or binary data.
78 // Note that the upper layers are left to verify the UTF-8 encoding.
79 // TODO(jiayl): prefer to use an enum instead of a bool.
80 bool binary;
81};
82
deadbeefb10f32f2017-02-08 09:38:2183// Used to implement RTCDataChannel events.
84//
85// The code responding to these callbacks should unwind the stack before
86// using any other webrtc APIs; re-entrancy is not supported.
henrike@webrtc.org28e20752013-07-10 00:45:3687class DataChannelObserver {
88 public:
89 // The data channel state have changed.
90 virtual void OnStateChange() = 0;
91 // A data buffer was successfully received.
92 virtual void OnMessage(const DataBuffer& buffer) = 0;
bemasc0edd50c2015-07-01 20:34:3393 // The data channel's buffered_amount has changed.
Marina Cioceae448a3f2019-03-04 14:52:2194 virtual void OnBufferedAmountChange(uint64_t sent_data_size) {}
henrike@webrtc.org28e20752013-07-10 00:45:3695
96 protected:
Mirko Bonadei79eb4dd2018-07-19 08:39:3097 virtual ~DataChannelObserver() = default;
henrike@webrtc.org28e20752013-07-10 00:45:3698};
99
Mirko Bonadei35214fc2019-09-23 12:54:28100class RTC_EXPORT DataChannelInterface : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36101 public:
deadbeefb10f32f2017-02-08 09:38:21102 // C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelstate
103 // Unlikely to change, but keep in sync with DataChannel.java:State and
tkchin@webrtc.orgff273322014-04-30 18:32:33104 // RTCDataChannel.h:RTCDataChannelState.
105 enum DataState {
henrike@webrtc.org28e20752013-07-10 00:45:36106 kConnecting,
107 kOpen, // The DataChannel is ready to send data.
108 kClosing,
109 kClosed
110 };
111
decurtis@webrtc.org487a4442015-01-15 22:55:07112 static const char* DataStateString(DataState state) {
113 switch (state) {
114 case kConnecting:
115 return "connecting";
116 case kOpen:
117 return "open";
118 case kClosing:
119 return "closing";
120 case kClosed:
121 return "closed";
122 }
henrikg91d6ede2015-09-17 07:24:34123 RTC_CHECK(false) << "Unknown DataChannel state: " << state;
decurtis@webrtc.org487a4442015-01-15 22:55:07124 return "";
125 }
126
deadbeefb10f32f2017-02-08 09:38:21127 // Used to receive events from the data channel. Only one observer can be
128 // registered at a time. UnregisterObserver should be called before the
129 // observer object is destroyed.
henrike@webrtc.org28e20752013-07-10 00:45:36130 virtual void RegisterObserver(DataChannelObserver* observer) = 0;
131 virtual void UnregisterObserver() = 0;
deadbeefb10f32f2017-02-08 09:38:21132
henrike@webrtc.org28e20752013-07-10 00:45:36133 // The label attribute represents a label that can be used to distinguish this
134 // DataChannel object from other DataChannel objects.
135 virtual std::string label() const = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54136
deadbeefb10f32f2017-02-08 09:38:21137 // The accessors below simply return the properties from the DataChannelInit
138 // the data channel was constructed with.
139 virtual bool reliable() const = 0;
140 // TODO(deadbeef): Remove these dummy implementations when all classes have
wu@webrtc.org822fbd82013-08-15 23:38:54141 // implemented these APIs. They should all just return the values the
142 // DataChannel was created with.
Mirko Bonadei79eb4dd2018-07-19 08:39:30143 virtual bool ordered() const;
Harald Alvestrandf3736ed2019-04-08 11:09:30144 // TODO(hta): Deprecate and remove the following two functions.
Mirko Bonadei79eb4dd2018-07-19 08:39:30145 virtual uint16_t maxRetransmitTime() const;
146 virtual uint16_t maxRetransmits() const;
Harald Alvestrandf3736ed2019-04-08 11:09:30147 virtual absl::optional<int> maxRetransmitsOpt() const;
148 virtual absl::optional<int> maxPacketLifeTime() const;
Mirko Bonadei79eb4dd2018-07-19 08:39:30149 virtual std::string protocol() const;
150 virtual bool negotiated() const;
wu@webrtc.org822fbd82013-08-15 23:38:54151
deadbeefb10f32f2017-02-08 09:38:21152 // Returns the ID from the DataChannelInit, if it was negotiated out-of-band.
153 // If negotiated in-band, this ID will be populated once the DTLS role is
154 // determined, and until then this will return -1.
henrike@webrtc.org28e20752013-07-10 00:45:36155 virtual int id() const = 0;
156 virtual DataState state() const = 0;
hbosc42d3762016-11-24 09:17:52157 virtual uint32_t messages_sent() const = 0;
158 virtual uint64_t bytes_sent() const = 0;
159 virtual uint32_t messages_received() const = 0;
160 virtual uint64_t bytes_received() const = 0;
deadbeefb10f32f2017-02-08 09:38:21161
162 // Returns the number of bytes of application data (UTF-8 text and binary
163 // data) that have been queued using Send but have not yet been processed at
164 // the SCTP level. See comment above Send below.
Peter Boström0c4e06b2015-10-07 10:23:21165 virtual uint64_t buffered_amount() const = 0;
deadbeefb10f32f2017-02-08 09:38:21166
167 // Begins the graceful data channel closing procedure. See:
168 // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.7
henrike@webrtc.org28e20752013-07-10 00:45:36169 virtual void Close() = 0;
deadbeefb10f32f2017-02-08 09:38:21170
171 // Sends |data| to the remote peer. If the data can't be sent at the SCTP
172 // level (due to congestion control), it's buffered at the data channel level,
173 // up to a maximum of 16MB. If Send is called while this buffer is full, the
174 // data channel will be closed abruptly.
175 //
176 // So, it's important to use buffered_amount() and OnBufferedAmountChange to
177 // ensure the data channel is used efficiently but without filling this
178 // buffer.
henrike@webrtc.org28e20752013-07-10 00:45:36179 virtual bool Send(const DataBuffer& buffer) = 0;
180
181 protected:
Mirko Bonadei79eb4dd2018-07-19 08:39:30182 ~DataChannelInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36183};
184
185} // namespace webrtc
186
Steve Anton10542f22019-01-11 17:11:00187#endif // API_DATA_CHANNEL_INTERFACE_H_