blob: 00e088635df62133aa4d02789f6ed5e236a4f526 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 * Copyright 2012 Google Inc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_APP_WEBRTC_DATACHANNEL_H_
29#define TALK_APP_WEBRTC_DATACHANNEL_H_
30
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +000031#include <deque>
deadbeefab9b2d12015-10-14 11:33:11 -070032#include <set>
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000033#include <string>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
35#include "talk/app/webrtc/datachannelinterface.h"
36#include "talk/app/webrtc/proxy.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000037#include "talk/session/media/channel.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000038#include "webrtc/base/messagehandler.h"
39#include "webrtc/base/scoped_ref_ptr.h"
40#include "webrtc/base/sigslot.h"
kjellandera96e2d72016-02-04 23:52:28 -080041#include "webrtc/media/base/mediachannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
43namespace webrtc {
44
wu@webrtc.org78187522013-10-07 23:32:02 +000045class DataChannel;
46
47class DataChannelProviderInterface {
48 public:
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000049 // Sends the data to the transport.
wu@webrtc.org78187522013-10-07 23:32:02 +000050 virtual bool SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000051 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +000052 cricket::SendDataResult* result) = 0;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000053 // Connects to the transport signals.
wu@webrtc.org78187522013-10-07 23:32:02 +000054 virtual bool ConnectDataChannel(DataChannel* data_channel) = 0;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000055 // Disconnects from the transport signals.
wu@webrtc.org78187522013-10-07 23:32:02 +000056 virtual void DisconnectDataChannel(DataChannel* data_channel) = 0;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000057 // Adds the data channel SID to the transport for SCTP.
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +000058 virtual void AddSctpDataStream(int sid) = 0;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000059 // Removes the data channel SID from the transport for SCTP.
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +000060 virtual void RemoveSctpDataStream(int sid) = 0;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000061 // Returns true if the transport channel is ready to send data.
62 virtual bool ReadyToSendData() const = 0;
wu@webrtc.org78187522013-10-07 23:32:02 +000063
64 protected:
65 virtual ~DataChannelProviderInterface() {}
66};
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000068struct InternalDataChannelInit : public DataChannelInit {
69 enum OpenHandshakeRole {
70 kOpener,
71 kAcker,
72 kNone
73 };
74 // The default role is kOpener because the default |negotiated| is false.
75 InternalDataChannelInit() : open_handshake_role(kOpener) {}
76 explicit InternalDataChannelInit(const DataChannelInit& base)
77 : DataChannelInit(base), open_handshake_role(kOpener) {
78 // If the channel is externally negotiated, do not send the OPEN message.
79 if (base.negotiated) {
80 open_handshake_role = kNone;
81 }
82 }
83
84 OpenHandshakeRole open_handshake_role;
85};
86
deadbeefab9b2d12015-10-14 11:33:11 -070087// Helper class to allocate unique IDs for SCTP DataChannels
88class SctpSidAllocator {
89 public:
90 // Gets the first unused odd/even id based on the DTLS role. If |role| is
91 // SSL_CLIENT, the allocated id starts from 0 and takes even numbers;
92 // otherwise, the id starts from 1 and takes odd numbers.
93 // Returns false if no id can be allocated.
94 bool AllocateSid(rtc::SSLRole role, int* sid);
95
96 // Attempts to reserve a specific sid. Returns false if it's unavailable.
97 bool ReserveSid(int sid);
98
99 // Indicates that |sid| isn't in use any more, and is thus available again.
100 void ReleaseSid(int sid);
101
102 private:
103 // Checks if |sid| is available to be assigned to a new SCTP data channel.
104 bool IsSidAvailable(int sid) const;
105
106 std::set<int> used_sids_;
107};
108
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109// DataChannel is a an implementation of the DataChannelInterface based on
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000110// libjingle's data engine. It provides an implementation of unreliable or
111// reliabledata channels. Currently this class is specifically designed to use
112// both RtpDataEngine and SctpDataEngine.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
114// DataChannel states:
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000115// kConnecting: The channel has been created the transport might not yet be
116// ready.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117// kOpen: The channel have a local SSRC set by a call to UpdateSendSsrc
118// and a remote SSRC set by call to UpdateReceiveSsrc and the transport
119// has been writable once.
120// kClosing: DataChannelInterface::Close has been called or UpdateReceiveSsrc
121// has been called with SSRC==0
122// kClosed: Both UpdateReceiveSsrc and UpdateSendSsrc has been called with
123// SSRC==0.
124class DataChannel : public DataChannelInterface,
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000125 public sigslot::has_slots<>,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000126 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000128 static rtc::scoped_refptr<DataChannel> Create(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000129 DataChannelProviderInterface* provider,
wu@webrtc.org78187522013-10-07 23:32:02 +0000130 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000132 const InternalDataChannelInit& config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133
134 virtual void RegisterObserver(DataChannelObserver* observer);
135 virtual void UnregisterObserver();
136
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000137 virtual std::string label() const { return label_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 virtual bool reliable() const;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000139 virtual bool ordered() const { return config_.ordered; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200140 virtual uint16_t maxRetransmitTime() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000141 return config_.maxRetransmitTime;
142 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200143 virtual uint16_t maxRetransmits() const { return config_.maxRetransmits; }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000144 virtual std::string protocol() const { return config_.protocol; }
145 virtual bool negotiated() const { return config_.negotiated; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 virtual int id() const { return config_.id; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200147 virtual uint64_t buffered_amount() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 virtual void Close();
149 virtual DataState state() const { return state_; }
150 virtual bool Send(const DataBuffer& buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000152 // rtc::MessageHandler override.
153 virtual void OnMessage(rtc::Message* msg);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000154
wu@webrtc.org91053e72013-08-10 07:18:04 +0000155 // Called when the channel's ready to use. That can happen when the
156 // underlying DataMediaChannel becomes ready, or when this channel is a new
157 // stream on an existing DataMediaChannel, and we've finished negotiation.
158 void OnChannelReady(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159
160 // Sigslots from cricket::DataChannel
161 void OnDataReceived(cricket::DataChannel* channel,
162 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000163 const rtc::Buffer& payload);
deadbeefab9b2d12015-10-14 11:33:11 -0700164 void OnStreamClosedRemotely(uint32_t sid);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000166 // The remote peer request that this channel should be closed.
167 void RemotePeerRequestClose();
168
169 // The following methods are for SCTP only.
170
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000171 // Sets the SCTP sid and adds to transport layer if not set yet. Should only
172 // be called once.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000173 void SetSctpSid(int sid);
174 // Called when the transport channel is created.
deadbeefab9b2d12015-10-14 11:33:11 -0700175 // Only needs to be called for SCTP data channels.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000176 void OnTransportChannelCreated();
deadbeefab9b2d12015-10-14 11:33:11 -0700177 // Called when the transport channel is destroyed.
178 void OnTransportChannelDestroyed();
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000179
180 // The following methods are for RTP only.
181
182 // Set the SSRC this channel should use to send data on the
183 // underlying data engine. |send_ssrc| == 0 means that the channel is no
184 // longer part of the session negotiation.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200185 void SetSendSsrc(uint32_t send_ssrc);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000186 // Set the SSRC this channel should use to receive data from the
187 // underlying data engine.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200188 void SetReceiveSsrc(uint32_t receive_ssrc);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000189
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000190 cricket::DataChannelType data_channel_type() const {
191 return data_channel_type_;
192 }
193
deadbeefab9b2d12015-10-14 11:33:11 -0700194 // Emitted when state transitions to kClosed.
195 // In the case of SCTP channels, this signal can be used to tell when the
196 // channel's sid is free.
197 sigslot::signal1<DataChannel*> SignalClosed;
198
wu@webrtc.org78187522013-10-07 23:32:02 +0000199 protected:
200 DataChannel(DataChannelProviderInterface* client,
201 cricket::DataChannelType dct,
202 const std::string& label);
203 virtual ~DataChannel();
204
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 private:
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000206 // A packet queue which tracks the total queued bytes. Queued packets are
207 // owned by this class.
208 class PacketQueue {
209 public:
210 PacketQueue();
211 ~PacketQueue();
212
213 size_t byte_count() const {
214 return byte_count_;
215 }
216
217 bool Empty() const;
218
219 DataBuffer* Front();
220
221 void Pop();
222
223 void Push(DataBuffer* packet);
224
225 void Clear();
226
227 void Swap(PacketQueue* other);
228
229 private:
230 std::deque<DataBuffer*> packets_;
231 size_t byte_count_;
232 };
233
Lally Singh5c6c6e02015-05-29 11:52:39 -0400234 // The OPEN(_ACK) signaling state.
235 enum HandshakeState {
236 kHandshakeInit,
237 kHandshakeShouldSendOpen,
238 kHandshakeShouldSendAck,
239 kHandshakeWaitingForAck,
240 kHandshakeReady
241 };
242
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000243 bool Init(const InternalDataChannelInit& config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 void DoClose();
245 void UpdateState();
246 void SetState(DataState state);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400247 void DisconnectFromProvider();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000248
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000249 void DeliverQueuedReceivedData();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000250
251 void SendQueuedDataMessages();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000252 bool SendDataMessage(const DataBuffer& buffer, bool queue_if_blocked);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000253 bool QueueSendDataMessage(const DataBuffer& buffer);
254
255 void SendQueuedControlMessages();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000256 void QueueControlMessage(const rtc::Buffer& buffer);
257 bool SendControlMessage(const rtc::Buffer& buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258
259 std::string label_;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000260 InternalDataChannelInit config_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 DataChannelObserver* observer_;
262 DataState state_;
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000263 cricket::DataChannelType data_channel_type_;
264 DataChannelProviderInterface* provider_;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400265 HandshakeState handshake_state_;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000266 bool connected_to_provider_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267 bool send_ssrc_set_;
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000268 bool receive_ssrc_set_;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400269 bool writable_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200270 uint32_t send_ssrc_;
271 uint32_t receive_ssrc_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000272 // Control messages that always have to get sent out before any queued
273 // data.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000274 PacketQueue queued_control_data_;
275 PacketQueue queued_received_data_;
276 PacketQueue queued_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277};
278
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279// Define proxy for DataChannelInterface.
280BEGIN_PROXY_MAP(DataChannel)
281 PROXY_METHOD1(void, RegisterObserver, DataChannelObserver*)
282 PROXY_METHOD0(void, UnregisterObserver)
283 PROXY_CONSTMETHOD0(std::string, label)
284 PROXY_CONSTMETHOD0(bool, reliable)
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000285 PROXY_CONSTMETHOD0(bool, ordered)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200286 PROXY_CONSTMETHOD0(uint16_t, maxRetransmitTime)
287 PROXY_CONSTMETHOD0(uint16_t, maxRetransmits)
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000288 PROXY_CONSTMETHOD0(std::string, protocol)
289 PROXY_CONSTMETHOD0(bool, negotiated)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 PROXY_CONSTMETHOD0(int, id)
291 PROXY_CONSTMETHOD0(DataState, state)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200292 PROXY_CONSTMETHOD0(uint64_t, buffered_amount)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293 PROXY_METHOD0(void, Close)
294 PROXY_METHOD1(bool, Send, const DataBuffer&)
295END_PROXY()
296
297} // namespace webrtc
298
299#endif // TALK_APP_WEBRTC_DATACHANNEL_H_