OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 // This file contains interfaces for DataChannels | 11 // This file contains interfaces for DataChannels |
12 // http://dev.w3.org/2011/webrtc/editor/webrtc.html#rtcdatachannel | 12 // http://dev.w3.org/2011/webrtc/editor/webrtc.html#rtcdatachannel |
13 | 13 |
14 #ifndef WEBRTC_API_DATACHANNELINTERFACE_H_ | 14 #ifndef WEBRTC_API_DATACHANNELINTERFACE_H_ |
15 #define WEBRTC_API_DATACHANNELINTERFACE_H_ | 15 #define WEBRTC_API_DATACHANNELINTERFACE_H_ |
16 | 16 |
17 #include <string> | 17 #include <string> |
18 | 18 |
19 #include "webrtc/base/basictypes.h" | 19 #include "webrtc/base/basictypes.h" |
20 #include "webrtc/base/checks.h" | 20 #include "webrtc/base/checks.h" |
21 #include "webrtc/base/copyonwritebuffer.h" | 21 #include "webrtc/base/copyonwritebuffer.h" |
22 #include "webrtc/base/refcount.h" | 22 #include "webrtc/base/refcount.h" |
23 | 23 |
24 | 24 |
25 namespace webrtc { | 25 namespace webrtc { |
26 | 26 |
| 27 // C++ version of: https://www.w3.org/TR/webrtc/#idl-def-rtcdatachannelinit |
| 28 // TODO(deadbeef): Use rtc::Optional for the "-1 if unset" things. |
27 struct DataChannelInit { | 29 struct DataChannelInit { |
28 DataChannelInit() | 30 // Deprecated. Reliability is assumed, and channel will be unreliable if |
29 : reliable(false), | 31 // maxRetransmitTime or MaxRetransmits is set. |
30 ordered(true), | 32 bool reliable = false; |
31 maxRetransmitTime(-1), | |
32 maxRetransmits(-1), | |
33 negotiated(false), | |
34 id(-1) { | |
35 } | |
36 | 33 |
37 bool reliable; // Deprecated. | 34 // True if ordered delivery is required. |
38 bool ordered; // True if ordered delivery is required. | 35 bool ordered = true; |
39 int maxRetransmitTime; // The max period of time in milliseconds in which | 36 |
40 // retransmissions will be sent. After this time, no | 37 // The max period of time in milliseconds in which retransmissions will be |
41 // more retransmissions will be sent. -1 if unset. | 38 // sent. After this time, no more retransmissions will be sent. -1 if unset. |
42 int maxRetransmits; // The max number of retransmissions. -1 if unset. | 39 // |
43 std::string protocol; // This is set by the application and opaque to the | 40 // Cannot be set along with |maxRetransmits|. |
44 // WebRTC implementation. | 41 int maxRetransmitTime = -1; |
45 bool negotiated; // True if the channel has been externally negotiated | 42 |
46 // and we do not send an in-band signalling in the | 43 // The max number of retransmissions. -1 if unset. |
47 // form of an "open" message. | 44 // |
48 int id; // The stream id, or SID, for SCTP data channels. -1 | 45 // Cannot be set along with |maxRetransmitTime|. |
49 // if unset. | 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; |
50 }; | 59 }; |
51 | 60 |
| 61 // 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. |
52 struct DataBuffer { | 64 struct DataBuffer { |
53 DataBuffer(const rtc::CopyOnWriteBuffer& data, bool binary) | 65 DataBuffer(const rtc::CopyOnWriteBuffer& data, bool binary) |
54 : data(data), | 66 : data(data), |
55 binary(binary) { | 67 binary(binary) { |
56 } | 68 } |
57 // For convenience for unit tests. | 69 // For convenience for unit tests. |
58 explicit DataBuffer(const std::string& text) | 70 explicit DataBuffer(const std::string& text) |
59 : data(text.data(), text.length()), | 71 : data(text.data(), text.length()), |
60 binary(false) { | 72 binary(false) { |
61 } | 73 } |
62 size_t size() const { return data.size(); } | 74 size_t size() const { return data.size(); } |
63 | 75 |
64 rtc::CopyOnWriteBuffer data; | 76 rtc::CopyOnWriteBuffer data; |
65 // Indicates if the received data contains UTF-8 or binary data. | 77 // 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. | 78 // 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. | 79 // TODO(jiayl): prefer to use an enum instead of a bool. |
68 bool binary; | 80 bool binary; |
69 }; | 81 }; |
70 | 82 |
| 83 // 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. |
71 class DataChannelObserver { | 87 class DataChannelObserver { |
72 public: | 88 public: |
73 // The data channel state have changed. | 89 // The data channel state have changed. |
74 virtual void OnStateChange() = 0; | 90 virtual void OnStateChange() = 0; |
75 // A data buffer was successfully received. | 91 // A data buffer was successfully received. |
76 virtual void OnMessage(const DataBuffer& buffer) = 0; | 92 virtual void OnMessage(const DataBuffer& buffer) = 0; |
77 // The data channel's buffered_amount has changed. | 93 // The data channel's buffered_amount has changed. |
78 virtual void OnBufferedAmountChange(uint64_t previous_amount) {} | 94 virtual void OnBufferedAmountChange(uint64_t previous_amount) {} |
79 | 95 |
80 protected: | 96 protected: |
81 virtual ~DataChannelObserver() {} | 97 virtual ~DataChannelObserver() {} |
82 }; | 98 }; |
83 | 99 |
84 class DataChannelInterface : public rtc::RefCountInterface { | 100 class DataChannelInterface : public rtc::RefCountInterface { |
85 public: | 101 public: |
86 // Keep in sync with DataChannel.java:State and | 102 // 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 |
87 // RTCDataChannel.h:RTCDataChannelState. | 104 // RTCDataChannel.h:RTCDataChannelState. |
88 enum DataState { | 105 enum DataState { |
89 kConnecting, | 106 kConnecting, |
90 kOpen, // The DataChannel is ready to send data. | 107 kOpen, // The DataChannel is ready to send data. |
91 kClosing, | 108 kClosing, |
92 kClosed | 109 kClosed |
93 }; | 110 }; |
94 | 111 |
95 static const char* DataStateString(DataState state) { | 112 static const char* DataStateString(DataState state) { |
96 switch (state) { | 113 switch (state) { |
97 case kConnecting: | 114 case kConnecting: |
98 return "connecting"; | 115 return "connecting"; |
99 case kOpen: | 116 case kOpen: |
100 return "open"; | 117 return "open"; |
101 case kClosing: | 118 case kClosing: |
102 return "closing"; | 119 return "closing"; |
103 case kClosed: | 120 case kClosed: |
104 return "closed"; | 121 return "closed"; |
105 } | 122 } |
106 RTC_CHECK(false) << "Unknown DataChannel state: " << state; | 123 RTC_CHECK(false) << "Unknown DataChannel state: " << state; |
107 return ""; | 124 return ""; |
108 } | 125 } |
109 | 126 |
| 127 // 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. |
110 virtual void RegisterObserver(DataChannelObserver* observer) = 0; | 130 virtual void RegisterObserver(DataChannelObserver* observer) = 0; |
111 virtual void UnregisterObserver() = 0; | 131 virtual void UnregisterObserver() = 0; |
| 132 |
112 // The label attribute represents a label that can be used to distinguish this | 133 // The label attribute represents a label that can be used to distinguish this |
113 // DataChannel object from other DataChannel objects. | 134 // DataChannel object from other DataChannel objects. |
114 virtual std::string label() const = 0; | 135 virtual std::string label() const = 0; |
| 136 |
| 137 // The accessors below simply return the properties from the DataChannelInit |
| 138 // the data channel was constructed with. |
115 virtual bool reliable() const = 0; | 139 virtual bool reliable() const = 0; |
116 | 140 // TODO(deadbeef): Remove these dummy implementations when all classes have |
117 // TODO(tommyw): Remove these dummy implementations when all classes have | |
118 // implemented these APIs. They should all just return the values the | 141 // implemented these APIs. They should all just return the values the |
119 // DataChannel was created with. | 142 // DataChannel was created with. |
120 virtual bool ordered() const { return false; } | 143 virtual bool ordered() const { return false; } |
121 virtual uint16_t maxRetransmitTime() const { return 0; } | 144 virtual uint16_t maxRetransmitTime() const { return 0; } |
122 virtual uint16_t maxRetransmits() const { return 0; } | 145 virtual uint16_t maxRetransmits() const { return 0; } |
123 virtual std::string protocol() const { return std::string(); } | 146 virtual std::string protocol() const { return std::string(); } |
124 virtual bool negotiated() const { return false; } | 147 virtual bool negotiated() const { return false; } |
125 | 148 |
| 149 // Returns the ID from the DataChannelInit, if it was negotiated out-of-band. |
| 150 // If negotiated in-band, this ID will be populated once the DTLS role is |
| 151 // determined, and until then this will return -1. |
126 virtual int id() const = 0; | 152 virtual int id() const = 0; |
127 virtual DataState state() const = 0; | 153 virtual DataState state() const = 0; |
128 virtual uint32_t messages_sent() const = 0; | 154 virtual uint32_t messages_sent() const = 0; |
129 virtual uint64_t bytes_sent() const = 0; | 155 virtual uint64_t bytes_sent() const = 0; |
130 virtual uint32_t messages_received() const = 0; | 156 virtual uint32_t messages_received() const = 0; |
131 virtual uint64_t bytes_received() const = 0; | 157 virtual uint64_t bytes_received() const = 0; |
132 // The buffered_amount returns the number of bytes of application data | 158 |
133 // (UTF-8 text and binary data) that have been queued using SendBuffer but | 159 // Returns the number of bytes of application data (UTF-8 text and binary |
134 // have not yet been transmitted to the network. | 160 // data) that have been queued using Send but have not yet been processed at |
| 161 // the SCTP level. See comment above Send below. |
135 virtual uint64_t buffered_amount() const = 0; | 162 virtual uint64_t buffered_amount() const = 0; |
| 163 |
| 164 // Begins the graceful data channel closing procedure. See: |
| 165 // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.7 |
136 virtual void Close() = 0; | 166 virtual void Close() = 0; |
137 // Sends |data| to the remote peer. | 167 |
| 168 // Sends |data| to the remote peer. If the data can't be sent at the SCTP |
| 169 // level (due to congestion control), it's buffered at the data channel level, |
| 170 // up to a maximum of 16MB. If Send is called while this buffer is full, the |
| 171 // data channel will be closed abruptly. |
| 172 // |
| 173 // So, it's important to use buffered_amount() and OnBufferedAmountChange to |
| 174 // ensure the data channel is used efficiently but without filling this |
| 175 // buffer. |
138 virtual bool Send(const DataBuffer& buffer) = 0; | 176 virtual bool Send(const DataBuffer& buffer) = 0; |
139 | 177 |
140 protected: | 178 protected: |
141 virtual ~DataChannelInterface() {} | 179 virtual ~DataChannelInterface() {} |
142 }; | 180 }; |
143 | 181 |
144 } // namespace webrtc | 182 } // namespace webrtc |
145 | 183 |
146 #endif // WEBRTC_API_DATACHANNELINTERFACE_H_ | 184 #endif // WEBRTC_API_DATACHANNELINTERFACE_H_ |
OLD | NEW |