blob: 1bd874e9361b491711a67b72583f3041357d4451 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/copy_on_write_buffer.h"
23#include "rtc_base/ref_count.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
25namespace webrtc {
26
deadbeefb10f32f2017-02-08 01:38:21 -080027// C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelinit
Danil Chapovalov0bc58cf2018-06-21 13:32:56 +020028// TODO(deadbeef): Use absl::optional for the "-1 if unset" things.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029struct DataChannelInit {
deadbeefb10f32f2017-02-08 01:38:21 -080030 // Deprecated. Reliability is assumed, and channel will be unreliable if
31 // maxRetransmitTime or MaxRetransmits is set.
32 bool reliable = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033
deadbeefb10f32f2017-02-08 01:38:21 -080034 // True if ordered delivery is required.
35 bool ordered = true;
36
37 // The max period of time in milliseconds in which retransmissions will be
38 // sent. After this time, no more retransmissions will be sent. -1 if unset.
39 //
40 // Cannot be set along with |maxRetransmits|.
41 int maxRetransmitTime = -1;
42
43 // The max number of retransmissions. -1 if unset.
44 //
45 // Cannot be set along with |maxRetransmitTime|.
46 int maxRetransmits = -1;
47
48 // This is set by the application and opaque to the WebRTC implementation.
49 std::string protocol;
50
51 // True if the channel has been externally negotiated and we do not send an
52 // in-band signalling in the form of an "open" message. If this is true, |id|
53 // below must be set; otherwise it should be unset and will be negotiated
54 // in-band.
55 bool negotiated = false;
56
57 // The stream id, or SID, for SCTP data channels. -1 if unset (see above).
58 int id = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059};
60
deadbeefb10f32f2017-02-08 01:38:21 -080061// At the JavaScript level, data can be passed in as a string or a blob, so
62// this structure's |binary| flag tells whether the data should be interpreted
63// as binary or text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064struct DataBuffer {
jbaucheec21bd2016-03-20 06:15:43 -070065 DataBuffer(const rtc::CopyOnWriteBuffer& data, bool binary)
Yves Gerey665174f2018-06-19 15:03:05 +020066 : data(data), binary(binary) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 // For convenience for unit tests.
68 explicit DataBuffer(const std::string& text)
Yves Gerey665174f2018-06-19 15:03:05 +020069 : data(text.data(), text.length()), binary(false) {}
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +000070 size_t size() const { return data.size(); }
wu@webrtc.orgd64719d2013-08-01 00:00:07 +000071
jbaucheec21bd2016-03-20 06:15:43 -070072 rtc::CopyOnWriteBuffer data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 // Indicates if the received data contains UTF-8 or binary data.
74 // Note that the upper layers are left to verify the UTF-8 encoding.
75 // TODO(jiayl): prefer to use an enum instead of a bool.
76 bool binary;
77};
78
deadbeefb10f32f2017-02-08 01:38:21 -080079// Used to implement RTCDataChannel events.
80//
81// The code responding to these callbacks should unwind the stack before
82// using any other webrtc APIs; re-entrancy is not supported.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083class DataChannelObserver {
84 public:
85 // The data channel state have changed.
86 virtual void OnStateChange() = 0;
87 // A data buffer was successfully received.
88 virtual void OnMessage(const DataBuffer& buffer) = 0;
bemasc0edd50c2015-07-01 13:34:33 -070089 // The data channel's buffered_amount has changed.
Marina Cioceae448a3f2019-03-04 15:52:21 +010090 virtual void OnBufferedAmountChange(uint64_t sent_data_size) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091
92 protected:
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +020093 virtual ~DataChannelObserver() = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094};
95
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000096class DataChannelInterface : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 public:
deadbeefb10f32f2017-02-08 01:38:21 -080098 // C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelstate
99 // Unlikely to change, but keep in sync with DataChannel.java:State and
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000100 // RTCDataChannel.h:RTCDataChannelState.
101 enum DataState {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 kConnecting,
103 kOpen, // The DataChannel is ready to send data.
104 kClosing,
105 kClosed
106 };
107
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000108 static const char* DataStateString(DataState state) {
109 switch (state) {
110 case kConnecting:
111 return "connecting";
112 case kOpen:
113 return "open";
114 case kClosing:
115 return "closing";
116 case kClosed:
117 return "closed";
118 }
henrikg91d6ede2015-09-17 00:24:34 -0700119 RTC_CHECK(false) << "Unknown DataChannel state: " << state;
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000120 return "";
121 }
122
deadbeefb10f32f2017-02-08 01:38:21 -0800123 // Used to receive events from the data channel. Only one observer can be
124 // registered at a time. UnregisterObserver should be called before the
125 // observer object is destroyed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 virtual void RegisterObserver(DataChannelObserver* observer) = 0;
127 virtual void UnregisterObserver() = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800128
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 // The label attribute represents a label that can be used to distinguish this
130 // DataChannel object from other DataChannel objects.
131 virtual std::string label() const = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000132
deadbeefb10f32f2017-02-08 01:38:21 -0800133 // The accessors below simply return the properties from the DataChannelInit
134 // the data channel was constructed with.
135 virtual bool reliable() const = 0;
136 // TODO(deadbeef): Remove these dummy implementations when all classes have
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000137 // implemented these APIs. They should all just return the values the
138 // DataChannel was created with.
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +0200139 virtual bool ordered() const;
140 virtual uint16_t maxRetransmitTime() const;
141 virtual uint16_t maxRetransmits() const;
142 virtual std::string protocol() const;
143 virtual bool negotiated() const;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000144
deadbeefb10f32f2017-02-08 01:38:21 -0800145 // Returns the ID from the DataChannelInit, if it was negotiated out-of-band.
146 // If negotiated in-band, this ID will be populated once the DTLS role is
147 // determined, and until then this will return -1.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 virtual int id() const = 0;
149 virtual DataState state() const = 0;
hbosc42d3762016-11-24 01:17:52 -0800150 virtual uint32_t messages_sent() const = 0;
151 virtual uint64_t bytes_sent() const = 0;
152 virtual uint32_t messages_received() const = 0;
153 virtual uint64_t bytes_received() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800154
155 // Returns the number of bytes of application data (UTF-8 text and binary
156 // data) that have been queued using Send but have not yet been processed at
157 // the SCTP level. See comment above Send below.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200158 virtual uint64_t buffered_amount() const = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800159
160 // Begins the graceful data channel closing procedure. See:
161 // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.7
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 virtual void Close() = 0;
deadbeefb10f32f2017-02-08 01:38:21 -0800163
164 // Sends |data| to the remote peer. If the data can't be sent at the SCTP
165 // level (due to congestion control), it's buffered at the data channel level,
166 // up to a maximum of 16MB. If Send is called while this buffer is full, the
167 // data channel will be closed abruptly.
168 //
169 // So, it's important to use buffered_amount() and OnBufferedAmountChange to
170 // ensure the data channel is used efficiently but without filling this
171 // buffer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 virtual bool Send(const DataBuffer& buffer) = 0;
173
174 protected:
Mirko Bonadei79eb4dd2018-07-19 10:39:30 +0200175 ~DataChannelInterface() override = default;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176};
177
178} // namespace webrtc
179
Steve Anton10542f22019-01-11 09:11:00 -0800180#endif // API_DATA_CHANNEL_INTERFACE_H_