blob: 0d67293db4ff166e46610a2d710aceb0b36831b6 [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"
36#include "talk/base/scoped_ref_ptr.h"
37#include "talk/base/sigslot.h"
wu@webrtc.org78187522013-10-07 23:32:02 +000038#include "talk/media/base/mediachannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "talk/session/media/channel.h"
40
41namespace webrtc {
42
wu@webrtc.org78187522013-10-07 23:32:02 +000043class DataChannel;
44
45class DataChannelProviderInterface {
46 public:
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000047 // Sends the data to the transport.
wu@webrtc.org78187522013-10-07 23:32:02 +000048 virtual bool SendData(const cricket::SendDataParams& params,
49 const talk_base::Buffer& payload,
50 cricket::SendDataResult* result) = 0;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000051 // Connects to the transport signals.
wu@webrtc.org78187522013-10-07 23:32:02 +000052 virtual bool ConnectDataChannel(DataChannel* data_channel) = 0;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000053 // Disconnects from the transport signals.
wu@webrtc.org78187522013-10-07 23:32:02 +000054 virtual void DisconnectDataChannel(DataChannel* data_channel) = 0;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000055 // Adds the send and receive stream ssrc to the transport for RTP.
56 virtual void AddRtpDataStream(uint32 send_ssrc, uint32 recv_ssrc) = 0;
57 // Adds the data channel SID to the transport for SCTP.
58 virtual void AddSctpDataStream(uint32 sid) = 0;
59 // Removes the data channel ssrcs from the transport for RTP.
60 virtual void RemoveRtpDataStream(uint32 send_ssrc, uint32 recv_ssrc) = 0;
61 // Removes the data channel SID from the transport for SCTP.
62 virtual void RemoveSctpDataStream(uint32 sid) = 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
68// DataChannel is a an implementation of the DataChannelInterface based on
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000069// libjingle's data engine. It provides an implementation of unreliable or
70// reliabledata channels. Currently this class is specifically designed to use
71// both RtpDataEngine and SctpDataEngine.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072
73// DataChannel states:
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000074// kConnecting: The channel has been created the transport might not yet be
75// ready.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076// kOpen: The channel have a local SSRC set by a call to UpdateSendSsrc
77// and a remote SSRC set by call to UpdateReceiveSsrc and the transport
78// has been writable once.
79// kClosing: DataChannelInterface::Close has been called or UpdateReceiveSsrc
80// has been called with SSRC==0
81// kClosed: Both UpdateReceiveSsrc and UpdateSendSsrc has been called with
82// SSRC==0.
83class DataChannel : public DataChannelInterface,
84 public sigslot::has_slots<> {
85 public:
86 static talk_base::scoped_refptr<DataChannel> Create(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +000087 DataChannelProviderInterface* provider,
wu@webrtc.org78187522013-10-07 23:32:02 +000088 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 const std::string& label,
90 const DataChannelInit* config);
91
92 virtual void RegisterObserver(DataChannelObserver* observer);
93 virtual void UnregisterObserver();
94
wu@webrtc.org822fbd82013-08-15 23:38:54 +000095 virtual std::string label() const { return label_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 virtual bool reliable() const;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000097 virtual bool ordered() const { return config_.ordered; }
98 virtual uint16 maxRetransmitTime() const {
99 return config_.maxRetransmitTime;
100 }
101 virtual uint16 maxRetransmits() const {
102 return config_.maxRetransmits;
103 }
104 virtual std::string protocol() const { return config_.protocol; }
105 virtual bool negotiated() const { return config_.negotiated; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 virtual int id() const { return config_.id; }
107 virtual uint64 buffered_amount() const;
108 virtual void Close();
109 virtual DataState state() const { return state_; }
110 virtual bool Send(const DataBuffer& buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111
112 // Called if the underlying data engine is closing.
113 void OnDataEngineClose();
114
wu@webrtc.org91053e72013-08-10 07:18:04 +0000115 // Called when the channel's ready to use. That can happen when the
116 // underlying DataMediaChannel becomes ready, or when this channel is a new
117 // stream on an existing DataMediaChannel, and we've finished negotiation.
118 void OnChannelReady(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119
120 // Sigslots from cricket::DataChannel
121 void OnDataReceived(cricket::DataChannel* channel,
122 const cricket::ReceiveDataParams& params,
123 const talk_base::Buffer& payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000125 // The remote peer request that this channel should be closed.
126 void RemotePeerRequestClose();
127
128 // The following methods are for SCTP only.
129
130 // Sets the SCTP sid and adds to transport layer if not set yet.
131 void SetSctpSid(int sid);
132 // Called when the transport channel is created.
133 void OnTransportChannelCreated();
134
135 // The following methods are for RTP only.
136
137 // Set the SSRC this channel should use to send data on the
138 // underlying data engine. |send_ssrc| == 0 means that the channel is no
139 // longer part of the session negotiation.
140 void SetSendSsrc(uint32 send_ssrc);
141 // Set the SSRC this channel should use to receive data from the
142 // underlying data engine.
143 void SetReceiveSsrc(uint32 receive_ssrc);
144
wu@webrtc.org78187522013-10-07 23:32:02 +0000145 protected:
146 DataChannel(DataChannelProviderInterface* client,
147 cricket::DataChannelType dct,
148 const std::string& label);
149 virtual ~DataChannel();
150
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 private:
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000152 bool Init(const DataChannelInit* config);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 void DoClose();
154 void UpdateState();
155 void SetState(DataState state);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000156 void DisconnectFromTransport();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000157 void DeliverQueuedControlData();
158 void QueueControl(const talk_base::Buffer* buffer);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000159 void ClearQueuedControlData();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000160 void DeliverQueuedReceivedData();
161 void ClearQueuedReceivedData();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000162 void DeliverQueuedSendData();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000163 void ClearQueuedSendData();
164 bool InternalSendWithoutQueueing(const DataBuffer& buffer,
165 cricket::SendDataResult* send_result);
166 bool QueueSendData(const DataBuffer& buffer);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000167 bool SendOpenMessage(const talk_base::Buffer* buffer);
168
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169
170 std::string label_;
171 DataChannelInit config_;
172 DataChannelObserver* observer_;
173 DataState state_;
174 bool was_ever_writable_;
wu@webrtc.org78187522013-10-07 23:32:02 +0000175 bool connected_to_provider_;
176 cricket::DataChannelType data_channel_type_;
177 DataChannelProviderInterface* provider_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 bool send_ssrc_set_;
179 uint32 send_ssrc_;
180 bool receive_ssrc_set_;
181 uint32 receive_ssrc_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000182 // Control messages that always have to get sent out before any queued
183 // data.
184 std::queue<const talk_base::Buffer*> queued_control_data_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000185 std::queue<DataBuffer*> queued_received_data_;
186 std::deque<DataBuffer*> queued_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187};
188
189class DataChannelFactory {
190 public:
191 virtual talk_base::scoped_refptr<DataChannel> CreateDataChannel(
192 const std::string& label,
193 const DataChannelInit* config) = 0;
194
195 protected:
196 virtual ~DataChannelFactory() {}
197};
198
199// Define proxy for DataChannelInterface.
200BEGIN_PROXY_MAP(DataChannel)
201 PROXY_METHOD1(void, RegisterObserver, DataChannelObserver*)
202 PROXY_METHOD0(void, UnregisterObserver)
203 PROXY_CONSTMETHOD0(std::string, label)
204 PROXY_CONSTMETHOD0(bool, reliable)
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000205 PROXY_CONSTMETHOD0(bool, ordered)
206 PROXY_CONSTMETHOD0(uint16, maxRetransmitTime)
207 PROXY_CONSTMETHOD0(uint16, maxRetransmits)
208 PROXY_CONSTMETHOD0(std::string, protocol)
209 PROXY_CONSTMETHOD0(bool, negotiated)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 PROXY_CONSTMETHOD0(int, id)
211 PROXY_CONSTMETHOD0(DataState, state)
212 PROXY_CONSTMETHOD0(uint64, buffered_amount)
213 PROXY_METHOD0(void, Close)
214 PROXY_METHOD1(bool, Send, const DataBuffer&)
215END_PROXY()
216
217} // namespace webrtc
218
219#endif // TALK_APP_WEBRTC_DATACHANNEL_H_