Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: webrtc/api/datachannel.h

Issue 2514883002: Create //webrtc/api:libjingle_peerconnection_api + refactorings. (Closed)
Patch Set: Big move! Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
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
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.
9 */
10
11 #ifndef WEBRTC_API_DATACHANNEL_H_
12 #define WEBRTC_API_DATACHANNEL_H_
13
14 #include <deque>
15 #include <set>
16 #include <string>
17
18 #include "webrtc/api/datachannelinterface.h"
19 #include "webrtc/api/proxy.h"
20 #include "webrtc/base/messagehandler.h"
21 #include "webrtc/base/scoped_ref_ptr.h"
22 #include "webrtc/base/sigslot.h"
23 #include "webrtc/media/base/mediachannel.h"
24 #include "webrtc/pc/channel.h"
25
26 namespace webrtc {
27
28 class DataChannel;
29
30 class DataChannelProviderInterface {
31 public:
32 // Sends the data to the transport.
33 virtual bool SendData(const cricket::SendDataParams& params,
34 const rtc::CopyOnWriteBuffer& payload,
35 cricket::SendDataResult* result) = 0;
36 // Connects to the transport signals.
37 virtual bool ConnectDataChannel(DataChannel* data_channel) = 0;
38 // Disconnects from the transport signals.
39 virtual void DisconnectDataChannel(DataChannel* data_channel) = 0;
40 // Adds the data channel SID to the transport for SCTP.
41 virtual void AddSctpDataStream(int sid) = 0;
42 // Removes the data channel SID from the transport for SCTP.
43 virtual void RemoveSctpDataStream(int sid) = 0;
44 // Returns true if the transport channel is ready to send data.
45 virtual bool ReadyToSendData() const = 0;
46
47 protected:
48 virtual ~DataChannelProviderInterface() {}
49 };
50
51 struct InternalDataChannelInit : public DataChannelInit {
52 enum OpenHandshakeRole {
53 kOpener,
54 kAcker,
55 kNone
56 };
57 // The default role is kOpener because the default |negotiated| is false.
58 InternalDataChannelInit() : open_handshake_role(kOpener) {}
59 explicit InternalDataChannelInit(const DataChannelInit& base)
60 : DataChannelInit(base), open_handshake_role(kOpener) {
61 // If the channel is externally negotiated, do not send the OPEN message.
62 if (base.negotiated) {
63 open_handshake_role = kNone;
64 }
65 }
66
67 OpenHandshakeRole open_handshake_role;
68 };
69
70 // Helper class to allocate unique IDs for SCTP DataChannels
71 class SctpSidAllocator {
72 public:
73 // Gets the first unused odd/even id based on the DTLS role. If |role| is
74 // SSL_CLIENT, the allocated id starts from 0 and takes even numbers;
75 // otherwise, the id starts from 1 and takes odd numbers.
76 // Returns false if no id can be allocated.
77 bool AllocateSid(rtc::SSLRole role, int* sid);
78
79 // Attempts to reserve a specific sid. Returns false if it's unavailable.
80 bool ReserveSid(int sid);
81
82 // Indicates that |sid| isn't in use any more, and is thus available again.
83 void ReleaseSid(int sid);
84
85 private:
86 // Checks if |sid| is available to be assigned to a new SCTP data channel.
87 bool IsSidAvailable(int sid) const;
88
89 std::set<int> used_sids_;
90 };
91
92 // DataChannel is a an implementation of the DataChannelInterface based on
93 // libjingle's data engine. It provides an implementation of unreliable or
94 // reliabledata channels. Currently this class is specifically designed to use
95 // both RtpDataEngine and SctpDataEngine.
96
97 // DataChannel states:
98 // kConnecting: The channel has been created the transport might not yet be
99 // ready.
100 // kOpen: The channel have a local SSRC set by a call to UpdateSendSsrc
101 // and a remote SSRC set by call to UpdateReceiveSsrc and the transport
102 // has been writable once.
103 // kClosing: DataChannelInterface::Close has been called or UpdateReceiveSsrc
104 // has been called with SSRC==0
105 // kClosed: Both UpdateReceiveSsrc and UpdateSendSsrc has been called with
106 // SSRC==0.
107 class DataChannel : public DataChannelInterface,
108 public sigslot::has_slots<>,
109 public rtc::MessageHandler {
110 public:
111 static rtc::scoped_refptr<DataChannel> Create(
112 DataChannelProviderInterface* provider,
113 cricket::DataChannelType dct,
114 const std::string& label,
115 const InternalDataChannelInit& config);
116
117 virtual void RegisterObserver(DataChannelObserver* observer);
118 virtual void UnregisterObserver();
119
120 virtual std::string label() const { return label_; }
121 virtual bool reliable() const;
122 virtual bool ordered() const { return config_.ordered; }
123 virtual uint16_t maxRetransmitTime() const {
124 return config_.maxRetransmitTime;
125 }
126 virtual uint16_t maxRetransmits() const { return config_.maxRetransmits; }
127 virtual std::string protocol() const { return config_.protocol; }
128 virtual bool negotiated() const { return config_.negotiated; }
129 virtual int id() const { return config_.id; }
130 virtual uint64_t buffered_amount() const;
131 virtual void Close();
132 virtual DataState state() const { return state_; }
133 virtual uint32_t messages_sent() const { return messages_sent_; }
134 virtual uint64_t bytes_sent() const { return bytes_sent_; }
135 virtual uint32_t messages_received() const { return messages_received_; }
136 virtual uint64_t bytes_received() const { return bytes_received_; }
137 virtual bool Send(const DataBuffer& buffer);
138
139 // rtc::MessageHandler override.
140 virtual void OnMessage(rtc::Message* msg);
141
142 // Called when the channel's ready to use. That can happen when the
143 // underlying DataMediaChannel becomes ready, or when this channel is a new
144 // stream on an existing DataMediaChannel, and we've finished negotiation.
145 void OnChannelReady(bool writable);
146
147 // Sigslots from cricket::DataChannel
148 void OnDataReceived(cricket::DataChannel* channel,
149 const cricket::ReceiveDataParams& params,
150 const rtc::CopyOnWriteBuffer& payload);
151 void OnStreamClosedRemotely(uint32_t sid);
152
153 // The remote peer request that this channel should be closed.
154 void RemotePeerRequestClose();
155
156 // The following methods are for SCTP only.
157
158 // Sets the SCTP sid and adds to transport layer if not set yet. Should only
159 // be called once.
160 void SetSctpSid(int sid);
161 // Called when the transport channel is created.
162 // Only needs to be called for SCTP data channels.
163 void OnTransportChannelCreated();
164 // Called when the transport channel is destroyed.
165 // This method makes sure the DataChannel is disconnected and changes state
166 // to kClosed.
167 void OnTransportChannelDestroyed();
168
169 // The following methods are for RTP only.
170
171 // Set the SSRC this channel should use to send data on the
172 // underlying data engine. |send_ssrc| == 0 means that the channel is no
173 // longer part of the session negotiation.
174 void SetSendSsrc(uint32_t send_ssrc);
175 // Set the SSRC this channel should use to receive data from the
176 // underlying data engine.
177 void SetReceiveSsrc(uint32_t receive_ssrc);
178
179 cricket::DataChannelType data_channel_type() const {
180 return data_channel_type_;
181 }
182
183 // Emitted when state transitions to kOpen.
184 sigslot::signal1<DataChannel*> SignalOpened;
185 // Emitted when state transitions to kClosed.
186 // In the case of SCTP channels, this signal can be used to tell when the
187 // channel's sid is free.
188 sigslot::signal1<DataChannel*> SignalClosed;
189
190 protected:
191 DataChannel(DataChannelProviderInterface* client,
192 cricket::DataChannelType dct,
193 const std::string& label);
194 virtual ~DataChannel();
195
196 private:
197 // A packet queue which tracks the total queued bytes. Queued packets are
198 // owned by this class.
199 class PacketQueue {
200 public:
201 PacketQueue();
202 ~PacketQueue();
203
204 size_t byte_count() const {
205 return byte_count_;
206 }
207
208 bool Empty() const;
209
210 DataBuffer* Front();
211
212 void Pop();
213
214 void Push(DataBuffer* packet);
215
216 void Clear();
217
218 void Swap(PacketQueue* other);
219
220 private:
221 std::deque<DataBuffer*> packets_;
222 size_t byte_count_;
223 };
224
225 // The OPEN(_ACK) signaling state.
226 enum HandshakeState {
227 kHandshakeInit,
228 kHandshakeShouldSendOpen,
229 kHandshakeShouldSendAck,
230 kHandshakeWaitingForAck,
231 kHandshakeReady
232 };
233
234 bool Init(const InternalDataChannelInit& config);
235 void DoClose();
236 void UpdateState();
237 void SetState(DataState state);
238 void DisconnectFromProvider();
239
240 void DeliverQueuedReceivedData();
241
242 void SendQueuedDataMessages();
243 bool SendDataMessage(const DataBuffer& buffer, bool queue_if_blocked);
244 bool QueueSendDataMessage(const DataBuffer& buffer);
245
246 void SendQueuedControlMessages();
247 void QueueControlMessage(const rtc::CopyOnWriteBuffer& buffer);
248 bool SendControlMessage(const rtc::CopyOnWriteBuffer& buffer);
249
250 std::string label_;
251 InternalDataChannelInit config_;
252 DataChannelObserver* observer_;
253 DataState state_;
254 uint32_t messages_sent_;
255 uint64_t bytes_sent_;
256 uint32_t messages_received_;
257 uint64_t bytes_received_;
258 cricket::DataChannelType data_channel_type_;
259 DataChannelProviderInterface* provider_;
260 HandshakeState handshake_state_;
261 bool connected_to_provider_;
262 bool send_ssrc_set_;
263 bool receive_ssrc_set_;
264 bool writable_;
265 uint32_t send_ssrc_;
266 uint32_t receive_ssrc_;
267 // Control messages that always have to get sent out before any queued
268 // data.
269 PacketQueue queued_control_data_;
270 PacketQueue queued_received_data_;
271 PacketQueue queued_send_data_;
272 };
273
274 // Define proxy for DataChannelInterface.
275 BEGIN_SIGNALING_PROXY_MAP(DataChannel)
276 PROXY_METHOD1(void, RegisterObserver, DataChannelObserver*)
277 PROXY_METHOD0(void, UnregisterObserver)
278 PROXY_CONSTMETHOD0(std::string, label)
279 PROXY_CONSTMETHOD0(bool, reliable)
280 PROXY_CONSTMETHOD0(bool, ordered)
281 PROXY_CONSTMETHOD0(uint16_t, maxRetransmitTime)
282 PROXY_CONSTMETHOD0(uint16_t, maxRetransmits)
283 PROXY_CONSTMETHOD0(std::string, protocol)
284 PROXY_CONSTMETHOD0(bool, negotiated)
285 PROXY_CONSTMETHOD0(int, id)
286 PROXY_CONSTMETHOD0(DataState, state)
287 PROXY_CONSTMETHOD0(uint32_t, messages_sent)
288 PROXY_CONSTMETHOD0(uint64_t, bytes_sent)
289 PROXY_CONSTMETHOD0(uint32_t, messages_received)
290 PROXY_CONSTMETHOD0(uint64_t, bytes_received)
291 PROXY_CONSTMETHOD0(uint64_t, buffered_amount)
292 PROXY_METHOD0(void, Close)
293 PROXY_METHOD1(bool, Send, const DataBuffer&)
294 END_SIGNALING_PROXY()
295
296 } // namespace webrtc
297
298 #endif // WEBRTC_API_DATACHANNEL_H_
OLDNEW
« webrtc/api/BUILD.gn ('K') | « webrtc/api/audiotrack.cc ('k') | webrtc/api/datachannel.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698