blob: 8b788c04684a70368fa16d6fb113a3ed14e11ce8 [file] [log] [blame]
Henrik Kjellanderbd0ae452016-02-10 09:53:121/*
kjellander95ed4e62016-02-10 15:54:432 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
Henrik Kjellanderbd0ae452016-02-10 09:53:123 *
kjellander95ed4e62016-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.
Henrik Kjellanderbd0ae452016-02-10 09:53:129 */
10
11// This file contains interfaces for DataChannels
12// http://dev.w3.org/2011/webrtc/editor/webrtc.html#rtcdatachannel
13
14#ifndef WEBRTC_API_DATACHANNELINTERFACE_H_
15#define WEBRTC_API_DATACHANNELINTERFACE_H_
16
17#include <string>
18
19#include "webrtc/base/basictypes.h"
Henrik Kjellanderbd0ae452016-02-10 09:53:1220#include "webrtc/base/checks.h"
jbauchbb6decf2016-03-20 13:15:4321#include "webrtc/base/copyonwritebuffer.h"
Henrik Kjellanderbd0ae452016-02-10 09:53:1222#include "webrtc/base/refcount.h"
23
24
25namespace webrtc {
26
27struct DataChannelInit {
28 DataChannelInit()
29 : reliable(false),
30 ordered(true),
31 maxRetransmitTime(-1),
32 maxRetransmits(-1),
33 negotiated(false),
34 id(-1) {
35 }
36
37 bool reliable; // Deprecated.
38 bool ordered; // True if ordered delivery is required.
39 int maxRetransmitTime; // The max period of time in milliseconds in which
40 // retransmissions will be sent. After this time, no
41 // more retransmissions will be sent. -1 if unset.
42 int maxRetransmits; // The max number of retransmissions. -1 if unset.
43 std::string protocol; // This is set by the application and opaque to the
44 // WebRTC implementation.
45 bool negotiated; // True if the channel has been externally negotiated
46 // and we do not send an in-band signalling in the
47 // form of an "open" message.
48 int id; // The stream id, or SID, for SCTP data channels. -1
49 // if unset.
50};
51
52struct DataBuffer {
jbauchbb6decf2016-03-20 13:15:4353 DataBuffer(const rtc::CopyOnWriteBuffer& data, bool binary)
Henrik Kjellanderbd0ae452016-02-10 09:53:1254 : data(data),
55 binary(binary) {
56 }
57 // For convenience for unit tests.
58 explicit DataBuffer(const std::string& text)
59 : data(text.data(), text.length()),
60 binary(false) {
61 }
62 size_t size() const { return data.size(); }
63
jbauchbb6decf2016-03-20 13:15:4364 rtc::CopyOnWriteBuffer data;
Henrik Kjellanderbd0ae452016-02-10 09:53:1265 // Indicates if the received data contains UTF-8 or binary data.
66 // Note that the upper layers are left to verify the UTF-8 encoding.
67 // TODO(jiayl): prefer to use an enum instead of a bool.
68 bool binary;
69};
70
71class DataChannelObserver {
72 public:
73 // The data channel state have changed.
74 virtual void OnStateChange() = 0;
75 // A data buffer was successfully received.
76 virtual void OnMessage(const DataBuffer& buffer) = 0;
77 // The data channel's buffered_amount has changed.
78 virtual void OnBufferedAmountChange(uint64_t previous_amount){};
79
80 protected:
81 virtual ~DataChannelObserver() {}
82};
83
84class DataChannelInterface : public rtc::RefCountInterface {
85 public:
86 // Keep in sync with DataChannel.java:State and
87 // RTCDataChannel.h:RTCDataChannelState.
88 enum DataState {
89 kConnecting,
90 kOpen, // The DataChannel is ready to send data.
91 kClosing,
92 kClosed
93 };
94
95 static const char* DataStateString(DataState state) {
96 switch (state) {
97 case kConnecting:
98 return "connecting";
99 case kOpen:
100 return "open";
101 case kClosing:
102 return "closing";
103 case kClosed:
104 return "closed";
105 }
106 RTC_CHECK(false) << "Unknown DataChannel state: " << state;
107 return "";
108 }
109
110 virtual void RegisterObserver(DataChannelObserver* observer) = 0;
111 virtual void UnregisterObserver() = 0;
112 // The label attribute represents a label that can be used to distinguish this
113 // DataChannel object from other DataChannel objects.
114 virtual std::string label() const = 0;
115 virtual bool reliable() const = 0;
116
117 // TODO(tommyw): Remove these dummy implementations when all classes have
118 // implemented these APIs. They should all just return the values the
119 // DataChannel was created with.
120 virtual bool ordered() const { return false; }
121 virtual uint16_t maxRetransmitTime() const { return 0; }
122 virtual uint16_t maxRetransmits() const { return 0; }
123 virtual std::string protocol() const { return std::string(); }
124 virtual bool negotiated() const { return false; }
125
126 virtual int id() const = 0;
127 virtual DataState state() const = 0;
hbos76d5ec82016-11-24 09:17:52128 virtual uint32_t messages_sent() const = 0;
129 virtual uint64_t bytes_sent() const = 0;
130 virtual uint32_t messages_received() const = 0;
131 virtual uint64_t bytes_received() const = 0;
Henrik Kjellanderbd0ae452016-02-10 09:53:12132 // The buffered_amount returns the number of bytes of application data
133 // (UTF-8 text and binary data) that have been queued using SendBuffer but
134 // have not yet been transmitted to the network.
135 virtual uint64_t buffered_amount() const = 0;
136 virtual void Close() = 0;
137 // Sends |data| to the remote peer.
138 virtual bool Send(const DataBuffer& buffer) = 0;
139
140 protected:
141 virtual ~DataChannelInterface() {}
142};
143
144} // namespace webrtc
145
146#endif // WEBRTC_API_DATACHANNELINTERFACE_H_