blob: 5635e63e61d1bbdbcd7a3fdec78a0496552c0034 [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 send and receive stream ssrc to the transport for RTP.
57 virtual void AddRtpDataStream(uint32 send_ssrc, uint32 recv_ssrc) = 0;
58 // Adds the data channel SID to the transport for SCTP.
59 virtual void AddSctpDataStream(uint32 sid) = 0;
60 // Removes the data channel ssrcs from the transport for RTP.
61 virtual void RemoveRtpDataStream(uint32 send_ssrc, uint32 recv_ssrc) = 0;
62 // Removes the data channel SID from the transport for SCTP.
63 virtual void RemoveSctpDataStream(uint32 sid) = 0;
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000064 // Returns true if the transport channel is ready to send data.
65 virtual bool ReadyToSendData() const = 0;
wu@webrtc.org78187522013-10-07 23:32:02 +000066
67 protected:
68 virtual ~DataChannelProviderInterface() {}
69};
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070
71// DataChannel is a an implementation of the DataChannelInterface based on
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000072// libjingle's data engine. It provides an implementation of unreliable or
73// reliabledata channels. Currently this class is specifically designed to use
74// both RtpDataEngine and SctpDataEngine.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075
76// DataChannel states:
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000077// kConnecting: The channel has been created the transport might not yet be
78// ready.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079// kOpen: The channel have a local SSRC set by a call to UpdateSendSsrc
80// and a remote SSRC set by call to UpdateReceiveSsrc and the transport
81// has been writable once.
82// kClosing: DataChannelInterface::Close has been called or UpdateReceiveSsrc
83// has been called with SSRC==0
84// kClosed: Both UpdateReceiveSsrc and UpdateSendSsrc has been called with
85// SSRC==0.
86class DataChannel : public DataChannelInterface,
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +000087 public sigslot::has_slots<>,
88 public talk_base::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 public:
90 static talk_base::scoped_refptr<DataChannel> Create(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000091 DataChannelProviderInterface* provider,
wu@webrtc.org78187522013-10-07 23:32:02 +000092 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 const std::string& label,
94 const DataChannelInit* config);
95
96 virtual void RegisterObserver(DataChannelObserver* observer);
97 virtual void UnregisterObserver();
98
wu@webrtc.org822fbd82013-08-15 23:38:54 +000099 virtual std::string label() const { return label_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 virtual bool reliable() const;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000101 virtual bool ordered() const { return config_.ordered; }
102 virtual uint16 maxRetransmitTime() const {
103 return config_.maxRetransmitTime;
104 }
105 virtual uint16 maxRetransmits() const {
106 return config_.maxRetransmits;
107 }
108 virtual std::string protocol() const { return config_.protocol; }
109 virtual bool negotiated() const { return config_.negotiated; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 virtual int id() const { return config_.id; }
111 virtual uint64 buffered_amount() const;
112 virtual void Close();
113 virtual DataState state() const { return state_; }
114 virtual bool Send(const DataBuffer& buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +0000116 // talk_base::MessageHandler override.
117 virtual void OnMessage(talk_base::Message* msg);
118
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 // Called if the underlying data engine is closing.
120 void OnDataEngineClose();
121
wu@webrtc.org91053e72013-08-10 07:18:04 +0000122 // Called when the channel's ready to use. That can happen when the
123 // underlying DataMediaChannel becomes ready, or when this channel is a new
124 // stream on an existing DataMediaChannel, and we've finished negotiation.
125 void OnChannelReady(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126
127 // Sigslots from cricket::DataChannel
128 void OnDataReceived(cricket::DataChannel* channel,
129 const cricket::ReceiveDataParams& params,
130 const talk_base::Buffer& payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000132 // The remote peer request that this channel should be closed.
133 void RemotePeerRequestClose();
134
135 // The following methods are for SCTP only.
136
137 // Sets the SCTP sid and adds to transport layer if not set yet.
138 void SetSctpSid(int sid);
139 // Called when the transport channel is created.
140 void OnTransportChannelCreated();
141
142 // The following methods are for RTP only.
143
144 // Set the SSRC this channel should use to send data on the
145 // underlying data engine. |send_ssrc| == 0 means that the channel is no
146 // longer part of the session negotiation.
147 void SetSendSsrc(uint32 send_ssrc);
148 // Set the SSRC this channel should use to receive data from the
149 // underlying data engine.
150 void SetReceiveSsrc(uint32 receive_ssrc);
151
wu@webrtc.org78187522013-10-07 23:32:02 +0000152 protected:
153 DataChannel(DataChannelProviderInterface* client,
154 cricket::DataChannelType dct,
155 const std::string& label);
156 virtual ~DataChannel();
157
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 private:
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000159 bool Init(const DataChannelInit* config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 void DoClose();
161 void UpdateState();
162 void SetState(DataState state);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000163 void DisconnectFromTransport();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000164 void DeliverQueuedControlData();
165 void QueueControl(const talk_base::Buffer* buffer);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000166 void ClearQueuedControlData();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000167 void DeliverQueuedReceivedData();
168 void ClearQueuedReceivedData();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000169 void DeliverQueuedSendData();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000170 void ClearQueuedSendData();
171 bool InternalSendWithoutQueueing(const DataBuffer& buffer,
172 cricket::SendDataResult* send_result);
173 bool QueueSendData(const DataBuffer& buffer);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000174 bool SendOpenMessage(const talk_base::Buffer* buffer);
175
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176
177 std::string label_;
178 DataChannelInit config_;
179 DataChannelObserver* observer_;
180 DataState state_;
181 bool was_ever_writable_;
wu@webrtc.org78187522013-10-07 23:32:02 +0000182 bool connected_to_provider_;
183 cricket::DataChannelType data_channel_type_;
184 DataChannelProviderInterface* provider_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 bool send_ssrc_set_;
186 uint32 send_ssrc_;
187 bool receive_ssrc_set_;
188 uint32 receive_ssrc_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000189 // Control messages that always have to get sent out before any queued
190 // data.
191 std::queue<const talk_base::Buffer*> queued_control_data_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000192 std::queue<DataBuffer*> queued_received_data_;
193 std::deque<DataBuffer*> queued_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194};
195
196class DataChannelFactory {
197 public:
198 virtual talk_base::scoped_refptr<DataChannel> CreateDataChannel(
199 const std::string& label,
200 const DataChannelInit* config) = 0;
201
202 protected:
203 virtual ~DataChannelFactory() {}
204};
205
206// Define proxy for DataChannelInterface.
207BEGIN_PROXY_MAP(DataChannel)
208 PROXY_METHOD1(void, RegisterObserver, DataChannelObserver*)
209 PROXY_METHOD0(void, UnregisterObserver)
210 PROXY_CONSTMETHOD0(std::string, label)
211 PROXY_CONSTMETHOD0(bool, reliable)
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000212 PROXY_CONSTMETHOD0(bool, ordered)
213 PROXY_CONSTMETHOD0(uint16, maxRetransmitTime)
214 PROXY_CONSTMETHOD0(uint16, maxRetransmits)
215 PROXY_CONSTMETHOD0(std::string, protocol)
216 PROXY_CONSTMETHOD0(bool, negotiated)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 PROXY_CONSTMETHOD0(int, id)
218 PROXY_CONSTMETHOD0(DataState, state)
219 PROXY_CONSTMETHOD0(uint64, buffered_amount)
220 PROXY_METHOD0(void, Close)
221 PROXY_METHOD1(bool, Send, const DataBuffer&)
222END_PROXY()
223
224} // namespace webrtc
225
226#endif // TALK_APP_WEBRTC_DATACHANNEL_H_