blob: 1a455b772c26cb054a21e210846668d4946fc5e4 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, Google Inc.
4 *
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.
57 virtual void AddSctpDataStream(uint32 sid) = 0;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000058 // Removes the data channel SID from the transport for SCTP.
59 virtual void RemoveSctpDataStream(uint32 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; }
117 virtual uint16 maxRetransmitTime() const {
118 return config_.maxRetransmitTime;
119 }
120 virtual uint16 maxRetransmits() const {
121 return config_.maxRetransmits;
122 }
123 virtual std::string protocol() const { return config_.protocol; }
124 virtual bool negotiated() const { return config_.negotiated; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 virtual int id() const { return config_.id; }
126 virtual uint64 buffered_amount() const;
127 virtual void Close();
128 virtual DataState state() const { return state_; }
129 virtual bool Send(const DataBuffer& buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000131 // rtc::MessageHandler override.
132 virtual void OnMessage(rtc::Message* msg);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000133
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 // Called if the underlying data engine is closing.
135 void OnDataEngineClose();
136
wu@webrtc.org91053e72013-08-10 07:18:04 +0000137 // Called when the channel's ready to use. That can happen when the
138 // underlying DataMediaChannel becomes ready, or when this channel is a new
139 // stream on an existing DataMediaChannel, and we've finished negotiation.
140 void OnChannelReady(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141
142 // Sigslots from cricket::DataChannel
143 void OnDataReceived(cricket::DataChannel* channel,
144 const cricket::ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000145 const rtc::Buffer& payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000147 // The remote peer request that this channel should be closed.
148 void RemotePeerRequestClose();
149
150 // The following methods are for SCTP only.
151
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000152 // Sets the SCTP sid and adds to transport layer if not set yet. Should only
153 // be called once.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000154 void SetSctpSid(int sid);
155 // Called when the transport channel is created.
156 void OnTransportChannelCreated();
157
158 // The following methods are for RTP only.
159
160 // Set the SSRC this channel should use to send data on the
161 // underlying data engine. |send_ssrc| == 0 means that the channel is no
162 // longer part of the session negotiation.
163 void SetSendSsrc(uint32 send_ssrc);
164 // Set the SSRC this channel should use to receive data from the
165 // underlying data engine.
166 void SetReceiveSsrc(uint32 receive_ssrc);
167
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000168 cricket::DataChannelType data_channel_type() const {
169 return data_channel_type_;
170 }
171
wu@webrtc.org78187522013-10-07 23:32:02 +0000172 protected:
173 DataChannel(DataChannelProviderInterface* client,
174 cricket::DataChannelType dct,
175 const std::string& label);
176 virtual ~DataChannel();
177
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 private:
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000179 // A packet queue which tracks the total queued bytes. Queued packets are
180 // owned by this class.
181 class PacketQueue {
182 public:
183 PacketQueue();
184 ~PacketQueue();
185
186 size_t byte_count() const {
187 return byte_count_;
188 }
189
190 bool Empty() const;
191
192 DataBuffer* Front();
193
194 void Pop();
195
196 void Push(DataBuffer* packet);
197
198 void Clear();
199
200 void Swap(PacketQueue* other);
201
202 private:
203 std::deque<DataBuffer*> packets_;
204 size_t byte_count_;
205 };
206
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000207 bool Init(const InternalDataChannelInit& config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 void DoClose();
209 void UpdateState();
210 void SetState(DataState state);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000211 void DisconnectFromTransport();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000212
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000213 void DeliverQueuedReceivedData();
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000214
215 void SendQueuedDataMessages();
jiayl@webrtc.org6ca61902014-11-12 17:28:40 +0000216 bool SendDataMessage(const DataBuffer& buffer, bool queue_if_blocked);
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000217 bool QueueSendDataMessage(const DataBuffer& buffer);
218
219 void SendQueuedControlMessages();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000220 void QueueControlMessage(const rtc::Buffer& buffer);
221 bool SendControlMessage(const rtc::Buffer& buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222
223 std::string label_;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000224 InternalDataChannelInit config_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 DataChannelObserver* observer_;
226 DataState state_;
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000227 cricket::DataChannelType data_channel_type_;
228 DataChannelProviderInterface* provider_;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000229 bool waiting_for_open_ack_;
230 bool was_ever_writable_;
231 bool connected_to_provider_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 bool send_ssrc_set_;
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000233 bool receive_ssrc_set_;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000234 uint32 send_ssrc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235 uint32 receive_ssrc_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000236 // Control messages that always have to get sent out before any queued
237 // data.
jiayl@webrtc.orgb43c99d2014-06-20 17:11:14 +0000238 PacketQueue queued_control_data_;
239 PacketQueue queued_received_data_;
240 PacketQueue queued_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241};
242
243class DataChannelFactory {
244 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000245 virtual rtc::scoped_refptr<DataChannel> CreateDataChannel(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000247 const InternalDataChannelInit* config) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000248
249 protected:
250 virtual ~DataChannelFactory() {}
251};
252
253// Define proxy for DataChannelInterface.
254BEGIN_PROXY_MAP(DataChannel)
255 PROXY_METHOD1(void, RegisterObserver, DataChannelObserver*)
256 PROXY_METHOD0(void, UnregisterObserver)
257 PROXY_CONSTMETHOD0(std::string, label)
258 PROXY_CONSTMETHOD0(bool, reliable)
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000259 PROXY_CONSTMETHOD0(bool, ordered)
260 PROXY_CONSTMETHOD0(uint16, maxRetransmitTime)
261 PROXY_CONSTMETHOD0(uint16, maxRetransmits)
262 PROXY_CONSTMETHOD0(std::string, protocol)
263 PROXY_CONSTMETHOD0(bool, negotiated)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 PROXY_CONSTMETHOD0(int, id)
265 PROXY_CONSTMETHOD0(DataState, state)
266 PROXY_CONSTMETHOD0(uint64, buffered_amount)
267 PROXY_METHOD0(void, Close)
268 PROXY_METHOD1(bool, Send, const DataBuffer&)
269END_PROXY()
270
271} // namespace webrtc
272
273#endif // TALK_APP_WEBRTC_DATACHANNEL_H_