blob: 9256e0e071ff039da674f789f95bd0477457fd1f [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
31#include <string>
32#include <queue>
33
34#include "talk/app/webrtc/datachannelinterface.h"
35#include "talk/app/webrtc/proxy.h"
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000036#include "talk/base/messagehandler.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037#include "talk/base/scoped_ref_ptr.h"
38#include "talk/base/sigslot.h"
wu@webrtc.org78187522013-10-07 23:32:02 +000039#include "talk/media/base/mediachannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040#include "talk/session/media/channel.h"
41
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,
50 const talk_base::Buffer& payload,
51 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<>,
103 public talk_base::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 public:
105 static talk_base::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
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000131 // talk_base::MessageHandler override.
132 virtual void OnMessage(talk_base::Message* msg);
133
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,
145 const talk_base::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
152 // Sets the SCTP sid and adds to transport layer if not set yet.
153 void SetSctpSid(int sid);
154 // Called when the transport channel is created.
155 void OnTransportChannelCreated();
156
157 // The following methods are for RTP only.
158
159 // Set the SSRC this channel should use to send data on the
160 // underlying data engine. |send_ssrc| == 0 means that the channel is no
161 // longer part of the session negotiation.
162 void SetSendSsrc(uint32 send_ssrc);
163 // Set the SSRC this channel should use to receive data from the
164 // underlying data engine.
165 void SetReceiveSsrc(uint32 receive_ssrc);
166
sergeyu@chromium.orga23f0ca2013-11-13 22:48:52 +0000167 cricket::DataChannelType data_channel_type() const {
168 return data_channel_type_;
169 }
170
wu@webrtc.org78187522013-10-07 23:32:02 +0000171 protected:
172 DataChannel(DataChannelProviderInterface* client,
173 cricket::DataChannelType dct,
174 const std::string& label);
175 virtual ~DataChannel();
176
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 private:
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000178 bool Init(const InternalDataChannelInit& config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 void DoClose();
180 void UpdateState();
181 void SetState(DataState state);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000182 void DisconnectFromTransport();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000183 void DeliverQueuedControlData();
184 void QueueControl(const talk_base::Buffer* buffer);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000185 void ClearQueuedControlData();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000186 void DeliverQueuedReceivedData();
187 void ClearQueuedReceivedData();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000188 void DeliverQueuedSendData();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000189 void ClearQueuedSendData();
190 bool InternalSendWithoutQueueing(const DataBuffer& buffer,
191 cricket::SendDataResult* send_result);
192 bool QueueSendData(const DataBuffer& buffer);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000193 bool SendOpenMessage(const talk_base::Buffer* buffer);
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000194 bool SendOpenAckMessage(const talk_base::Buffer* buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195
196 std::string label_;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000197 InternalDataChannelInit config_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 DataChannelObserver* observer_;
199 DataState state_;
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000200 cricket::DataChannelType data_channel_type_;
201 DataChannelProviderInterface* provider_;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000202 bool waiting_for_open_ack_;
203 bool was_ever_writable_;
204 bool connected_to_provider_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 bool send_ssrc_set_;
henrika@webrtc.org44461fa2014-01-13 09:35:02 +0000206 bool receive_ssrc_set_;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000207 uint32 send_ssrc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 uint32 receive_ssrc_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000209 // Control messages that always have to get sent out before any queued
210 // data.
211 std::queue<const talk_base::Buffer*> queued_control_data_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000212 std::queue<DataBuffer*> queued_received_data_;
213 std::deque<DataBuffer*> queued_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214};
215
216class DataChannelFactory {
217 public:
218 virtual talk_base::scoped_refptr<DataChannel> CreateDataChannel(
219 const std::string& label,
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +0000220 const InternalDataChannelInit* config) = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221
222 protected:
223 virtual ~DataChannelFactory() {}
224};
225
226// Define proxy for DataChannelInterface.
227BEGIN_PROXY_MAP(DataChannel)
228 PROXY_METHOD1(void, RegisterObserver, DataChannelObserver*)
229 PROXY_METHOD0(void, UnregisterObserver)
230 PROXY_CONSTMETHOD0(std::string, label)
231 PROXY_CONSTMETHOD0(bool, reliable)
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000232 PROXY_CONSTMETHOD0(bool, ordered)
233 PROXY_CONSTMETHOD0(uint16, maxRetransmitTime)
234 PROXY_CONSTMETHOD0(uint16, maxRetransmits)
235 PROXY_CONSTMETHOD0(std::string, protocol)
236 PROXY_CONSTMETHOD0(bool, negotiated)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 PROXY_CONSTMETHOD0(int, id)
238 PROXY_CONSTMETHOD0(DataState, state)
239 PROXY_CONSTMETHOD0(uint64, buffered_amount)
240 PROXY_METHOD0(void, Close)
241 PROXY_METHOD1(bool, Send, const DataBuffer&)
242END_PROXY()
243
244} // namespace webrtc
245
246#endif // TALK_APP_WEBRTC_DATACHANNEL_H_