blob: 4506f71b146c71f95ab22a04fbcffca97b43bb32 [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>
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000032#include <string>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033
34#include "talk/app/webrtc/datachannelinterface.h"
35#include "talk/app/webrtc/proxy.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000036#include "talk/media/base/mediachannel.h"
37#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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42namespace webrtc {
43
wu@webrtc.org78187522013-10-07 23:32:02 +000044class DataChannel;
45
46class DataChannelProviderInterface {
47 public:
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000048 // Sends the data to the transport.
wu@webrtc.org78187522013-10-07 23:32:02 +000049 virtual bool SendData(const cricket::SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000050 const rtc::Buffer& payload,
wu@webrtc.org78187522013-10-07 23:32:02 +000051 cricket::SendDataResult* result) = 0;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000052 // Connects to the transport signals.
wu@webrtc.org78187522013-10-07 23:32:02 +000053 virtual bool ConnectDataChannel(DataChannel* data_channel) = 0;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000054 // Disconnects from the transport signals.
wu@webrtc.org78187522013-10-07 23:32:02 +000055 virtual void DisconnectDataChannel(DataChannel* data_channel) = 0;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000056 // Adds the data channel SID to the transport for SCTP.
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +000057 virtual void AddSctpDataStream(int sid) = 0;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000058 // Removes the data channel SID from the transport for SCTP.
bemasc@webrtc.org9b5467e2014-12-04 23:16:52 +000059 virtual void RemoveSctpDataStream(int sid) = 0;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000060 // Returns true if the transport channel is ready to send data.
61 virtual bool ReadyToSendData() const = 0;
wu@webrtc.org78187522013-10-07 23:32:02 +000062
63 protected:
64 virtual ~DataChannelProviderInterface() {}
65};
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +000067struct InternalDataChannelInit : public DataChannelInit {
68 enum OpenHandshakeRole {
69 kOpener,
70 kAcker,
71 kNone
72 };
73 // The default role is kOpener because the default |negotiated| is false.
74 InternalDataChannelInit() : open_handshake_role(kOpener) {}
75 explicit InternalDataChannelInit(const DataChannelInit& base)
76 : DataChannelInit(base), open_handshake_role(kOpener) {
77 // If the channel is externally negotiated, do not send the OPEN message.
78 if (base.negotiated) {
79 open_handshake_role = kNone;
80 }
81 }
82
83 OpenHandshakeRole open_handshake_role;
84};
85
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086// DataChannel is a an implementation of the DataChannelInterface based on
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000087// libjingle's data engine. It provides an implementation of unreliable or
88// reliabledata channels. Currently this class is specifically designed to use
89// both RtpDataEngine and SctpDataEngine.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
91// DataChannel states:
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000092// kConnecting: The channel has been created the transport might not yet be
93// ready.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094// kOpen: The channel have a local SSRC set by a call to UpdateSendSsrc
95// and a remote SSRC set by call to UpdateReceiveSsrc and the transport
96// has been writable once.
97// kClosing: DataChannelInterface::Close has been called or UpdateReceiveSsrc
98// has been called with SSRC==0
99// kClosed: Both UpdateReceiveSsrc and UpdateSendSsrc has been called with
100// SSRC==0.
101class DataChannel : public DataChannelInterface,
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000102 public sigslot::has_slots<>,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000103 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000105 static rtc::scoped_refptr<DataChannel> Create(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000106 DataChannelProviderInterface* provider,
wu@webrtc.org78187522013-10-07 23:32:02 +0000107 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000109 const InternalDataChannelInit& config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110
111 virtual void RegisterObserver(DataChannelObserver* observer);
112 virtual void UnregisterObserver();
113
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000114 virtual std::string label() const { return label_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 virtual bool reliable() const;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000116 virtual bool ordered() const { return config_.ordered; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200117 virtual uint16_t maxRetransmitTime() const {
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000118 return config_.maxRetransmitTime;
119 }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200120 virtual uint16_t maxRetransmits() const { return config_.maxRetransmits; }
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000121 virtual std::string protocol() const { return config_.protocol; }
122 virtual bool negotiated() const { return config_.negotiated; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 virtual int id() const { return config_.id; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200124 virtual uint64_t buffered_amount() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 virtual void Close();
126 virtual DataState state() const { return state_; }
127 virtual bool Send(const DataBuffer& buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000129 // rtc::MessageHandler override.
130 virtual void OnMessage(rtc::Message* msg);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000131
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 // Called if the underlying data engine is closing.
133 void OnDataEngineClose();
134
wu@webrtc.org91053e72013-08-10 07:18:04 +0000135 // Called when the channel's ready to use. That can happen when the
136 // underlying DataMediaChannel becomes ready, or when this channel is a new
137 // stream on an existing DataMediaChannel, and we've finished negotiation.
138 void OnChannelReady(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139
140 // Sigslots from cricket::DataChannel
141 void OnDataReceived(cricket::DataChannel* channel,
142 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000143 const rtc::Buffer& payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000145 // The remote peer request that this channel should be closed.
146 void RemotePeerRequestClose();
147
148 // The following methods are for SCTP only.
149
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000150 // Sets the SCTP sid and adds to transport layer if not set yet. Should only
151 // be called once.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000152 void SetSctpSid(int sid);
153 // Called when the transport channel is created.
154 void OnTransportChannelCreated();
155
156 // The following methods are for RTP only.
157
158 // Set the SSRC this channel should use to send data on the
159 // underlying data engine. |send_ssrc| == 0 means that the channel is no
160 // longer part of the session negotiation.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200161 void SetSendSsrc(uint32_t send_ssrc);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000162 // Set the SSRC this channel should use to receive data from the
163 // underlying data engine.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200164 void SetReceiveSsrc(uint32_t receive_ssrc);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000165
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000166 cricket::DataChannelType data_channel_type() const {
167 return data_channel_type_;
168 }
169
wu@webrtc.org78187522013-10-07 23:32:02 +0000170 protected:
171 DataChannel(DataChannelProviderInterface* client,
172 cricket::DataChannelType dct,
173 const std::string& label);
174 virtual ~DataChannel();
175
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 private:
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000177 // A packet queue which tracks the total queued bytes. Queued packets are
178 // owned by this class.
179 class PacketQueue {
180 public:
181 PacketQueue();
182 ~PacketQueue();
183
184 size_t byte_count() const {
185 return byte_count_;
186 }
187
188 bool Empty() const;
189
190 DataBuffer* Front();
191
192 void Pop();
193
194 void Push(DataBuffer* packet);
195
196 void Clear();
197
198 void Swap(PacketQueue* other);
199
200 private:
201 std::deque<DataBuffer*> packets_;
202 size_t byte_count_;
203 };
204
Lally Singh5c6c6e02015-05-29 11:52:39 -0400205 // The OPEN(_ACK) signaling state.
206 enum HandshakeState {
207 kHandshakeInit,
208 kHandshakeShouldSendOpen,
209 kHandshakeShouldSendAck,
210 kHandshakeWaitingForAck,
211 kHandshakeReady
212 };
213
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000214 bool Init(const InternalDataChannelInit& config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 void DoClose();
216 void UpdateState();
217 void SetState(DataState state);
Lally Singh5c6c6e02015-05-29 11:52:39 -0400218 void DisconnectFromProvider();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000219
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000220 void DeliverQueuedReceivedData();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000221
222 void SendQueuedDataMessages();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000223 bool SendDataMessage(const DataBuffer& buffer, bool queue_if_blocked);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000224 bool QueueSendDataMessage(const DataBuffer& buffer);
225
226 void SendQueuedControlMessages();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000227 void QueueControlMessage(const rtc::Buffer& buffer);
228 bool SendControlMessage(const rtc::Buffer& buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229
230 std::string label_;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000231 InternalDataChannelInit config_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 DataChannelObserver* observer_;
233 DataState state_;
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000234 cricket::DataChannelType data_channel_type_;
235 DataChannelProviderInterface* provider_;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400236 HandshakeState handshake_state_;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000237 bool connected_to_provider_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 bool send_ssrc_set_;
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000239 bool receive_ssrc_set_;
Lally Singh5c6c6e02015-05-29 11:52:39 -0400240 bool writable_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200241 uint32_t send_ssrc_;
242 uint32_t receive_ssrc_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000243 // Control messages that always have to get sent out before any queued
244 // data.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000245 PacketQueue queued_control_data_;
246 PacketQueue queued_received_data_;
247 PacketQueue queued_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248};
249
250class DataChannelFactory {
251 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000252 virtual rtc::scoped_refptr<DataChannel> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000254 const InternalDataChannelInit* config) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255
256 protected:
257 virtual ~DataChannelFactory() {}
258};
259
260// Define proxy for DataChannelInterface.
261BEGIN_PROXY_MAP(DataChannel)
262 PROXY_METHOD1(void, RegisterObserver, DataChannelObserver*)
263 PROXY_METHOD0(void, UnregisterObserver)
264 PROXY_CONSTMETHOD0(std::string, label)
265 PROXY_CONSTMETHOD0(bool, reliable)
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000266 PROXY_CONSTMETHOD0(bool, ordered)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200267 PROXY_CONSTMETHOD0(uint16_t, maxRetransmitTime)
268 PROXY_CONSTMETHOD0(uint16_t, maxRetransmits)
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000269 PROXY_CONSTMETHOD0(std::string, protocol)
270 PROXY_CONSTMETHOD0(bool, negotiated)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271 PROXY_CONSTMETHOD0(int, id)
272 PROXY_CONSTMETHOD0(DataState, state)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200273 PROXY_CONSTMETHOD0(uint64_t, buffered_amount)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 PROXY_METHOD0(void, Close)
275 PROXY_METHOD1(bool, Send, const DataBuffer&)
276END_PROXY()
277
278} // namespace webrtc
279
280#endif // TALK_APP_WEBRTC_DATACHANNEL_H_