blob: a0d2b3b51a59858b1ba84fdeae8b69133d19cce9 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#ifndef API_DATACHANNELINTERFACE_H_
15#define API_DATACHANNELINTERFACE_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000016
17#include <string>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
20#include "rtc_base/copyonwritebuffer.h"
21#include "rtc_base/refcount.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23namespace webrtc {
24
deadbeefb10f32f2017-02-08 01:38:21 -080025// C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelinit
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020026// TODO(deadbeef): Use absl::optional for the "-1 if unset" things.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027struct DataChannelInit {
deadbeefb10f32f2017-02-08 01:38:21 -080028 // Deprecated. Reliability is assumed, and channel will be unreliable if
29 // maxRetransmitTime or MaxRetransmits is set.
30 bool reliable = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
deadbeefb10f32f2017-02-08 01:38:21 -080032 // True if ordered delivery is required.
33 bool ordered = true;
34
35 // The max period of time in milliseconds in which retransmissions will be
36 // sent. After this time, no more retransmissions will be sent. -1 if unset.
37 //
38 // Cannot be set along with |maxRetransmits|.
39 int maxRetransmitTime = -1;
40
41 // The max number of retransmissions. -1 if unset.
42 //
43 // Cannot be set along with |maxRetransmitTime|.
44 int maxRetransmits = -1;
45
46 // This is set by the application and opaque to the WebRTC implementation.
47 std::string protocol;
48
49 // True if the channel has been externally negotiated and we do not send an
50 // in-band signalling in the form of an "open" message. If this is true, |id|
51 // below must be set; otherwise it should be unset and will be negotiated
52 // in-band.
53 bool negotiated = false;
54
55 // The stream id, or SID, for SCTP data channels. -1 if unset (see above).
56 int id = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057};
58
deadbeefb10f32f2017-02-08 01:38:21 -080059// At the JavaScript level, data can be passed in as a string or a blob, so
60// this structure's |binary| flag tells whether the data should be interpreted
61// as binary or text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062struct DataBuffer {
jbaucheec21bd2016-03-20 06:15:43 -070063 DataBuffer(const rtc::CopyOnWriteBuffer& data, bool binary)
Yves Gerey665174f2018-06-19 15:03:05 +020064 : data(data), binary(binary) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 // For convenience for unit tests.
66 explicit DataBuffer(const std::string& text)
Yves Gerey665174f2018-06-19 15:03:05 +020067 : data(text.data(), text.length()), binary(false) {}
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +000068 size_t size() const { return data.size(); }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000069
jbaucheec21bd2016-03-20 06:15:43 -070070 rtc::CopyOnWriteBuffer data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 // Indicates if the received data contains UTF-8 or binary data.
72 // Note that the upper layers are left to verify the UTF-8 encoding.
73 // TODO(jiayl): prefer to use an enum instead of a bool.
74 bool binary;
75};
76
deadbeefb10f32f2017-02-08 01:38:21 -080077// Used to implement RTCDataChannel events.
78//
79// The code responding to these callbacks should unwind the stack before
80// using any other webrtc APIs; re-entrancy is not supported.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081class DataChannelObserver {
82 public:
83 // The data channel state have changed.
84 virtual void OnStateChange() = 0;
85 // A data buffer was successfully received.
86 virtual void OnMessage(const DataBuffer& buffer) = 0;
bemasc0edd50c2015-07-01 13:34:33 -070087 // The data channel's buffered_amount has changed.
oprypin803dc292017-02-01 01:55:59 -080088 virtual void OnBufferedAmountChange(uint64_t previous_amount) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089
90 protected:
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +020091 virtual ~DataChannelObserver() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092};
93
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000094class DataChannelInterface : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 public:
deadbeefb10f32f2017-02-08 01:38:21 -080096 // C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelstate
97 // Unlikely to change, but keep in sync with DataChannel.java:State and
tkchin@webrtc.orgff273322014-04-30 18:32:33 +000098 // RTCDataChannel.h:RTCDataChannelState.
99 enum DataState {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 kConnecting,
101 kOpen, // The DataChannel is ready to send data.
102 kClosing,
103 kClosed
104 };
105
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000106 static const char* DataStateString(DataState state) {
107 switch (state) {
108 case kConnecting:
109 return "connecting";
110 case kOpen:
111 return "open";
112 case kClosing:
113 return "closing";
114 case kClosed:
115 return "closed";
116 }
henrikg91d6ede2015-09-17 00:24:34 -0700117 RTC_CHECK(false) << "Unknown DataChannel state: " << state;
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000118 return "";
119 }
120
deadbeefb10f32f2017-02-08 01:38:21 -0800121 // Used to receive events from the data channel. Only one observer can be
122 // registered at a time. UnregisterObserver should be called before the
123 // observer object is destroyed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 virtual void RegisterObserver(DataChannelObserver* observer) = 0;
125 virtual void UnregisterObserver() = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800126
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 // The label attribute represents a label that can be used to distinguish this
128 // DataChannel object from other DataChannel objects.
129 virtual std::string label() const = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000130
deadbeefb10f32f2017-02-08 01:38:21 -0800131 // The accessors below simply return the properties from the DataChannelInit
132 // the data channel was constructed with.
133 virtual bool reliable() const = 0;
134 // TODO(deadbeef): Remove these dummy implementations when all classes have
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000135 // implemented these APIs. They should all just return the values the
136 // DataChannel was created with.
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +0200137 virtual bool ordered() const;
138 virtual uint16_t maxRetransmitTime() const;
139 virtual uint16_t maxRetransmits() const;
140 virtual std::string protocol() const;
141 virtual bool negotiated() const;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000142
deadbeefb10f32f2017-02-08 01:38:21 -0800143 // Returns the ID from the DataChannelInit, if it was negotiated out-of-band.
144 // If negotiated in-band, this ID will be populated once the DTLS role is
145 // determined, and until then this will return -1.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 virtual int id() const = 0;
147 virtual DataState state() const = 0;
hbosc42d3762016-11-24 01:17:52 -0800148 virtual uint32_t messages_sent() const = 0;
149 virtual uint64_t bytes_sent() const = 0;
150 virtual uint32_t messages_received() const = 0;
151 virtual uint64_t bytes_received() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800152
153 // Returns the number of bytes of application data (UTF-8 text and binary
154 // data) that have been queued using Send but have not yet been processed at
155 // the SCTP level. See comment above Send below.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200156 virtual uint64_t buffered_amount() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800157
158 // Begins the graceful data channel closing procedure. See:
159 // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.7
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 virtual void Close() = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800161
162 // Sends |data| to the remote peer. If the data can't be sent at the SCTP
163 // level (due to congestion control), it's buffered at the data channel level,
164 // up to a maximum of 16MB. If Send is called while this buffer is full, the
165 // data channel will be closed abruptly.
166 //
167 // So, it's important to use buffered_amount() and OnBufferedAmountChange to
168 // ensure the data channel is used efficiently but without filling this
169 // buffer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 virtual bool Send(const DataBuffer& buffer) = 0;
171
172 protected:
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +0200173 ~DataChannelInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174};
175
176} // namespace webrtc
177
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200178#endif // API_DATACHANNELINTERFACE_H_