blob: a09c7f6d61195f2d9438844eb4b9a8cb9d13b812 [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:
47 virtual bool SendData(const cricket::SendDataParams& params,
48 const talk_base::Buffer& payload,
49 cricket::SendDataResult* result) = 0;
50 virtual bool ConnectDataChannel(DataChannel* data_channel) = 0;
51 virtual void DisconnectDataChannel(DataChannel* data_channel) = 0;
52
53 protected:
54 virtual ~DataChannelProviderInterface() {}
55};
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056
57// DataChannel is a an implementation of the DataChannelInterface based on
58// libjingle's data engine. It provides an implementation of unreliable data
59// channels. Currently this class is specifically designed to use RtpDataEngine,
60// and will changed to use SCTP in the future.
61
62// DataChannel states:
63// kConnecting: The channel has been created but SSRC for sending and receiving
64// has not yet been set and the transport might not yet be ready.
65// kOpen: The channel have a local SSRC set by a call to UpdateSendSsrc
66// and a remote SSRC set by call to UpdateReceiveSsrc and the transport
67// has been writable once.
68// kClosing: DataChannelInterface::Close has been called or UpdateReceiveSsrc
69// has been called with SSRC==0
70// kClosed: Both UpdateReceiveSsrc and UpdateSendSsrc has been called with
71// SSRC==0.
72class DataChannel : public DataChannelInterface,
73 public sigslot::has_slots<> {
74 public:
75 static talk_base::scoped_refptr<DataChannel> Create(
wu@webrtc.org78187522013-10-07 23:32:02 +000076 DataChannelProviderInterface* client,
77 cricket::DataChannelType dct,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 const std::string& label,
79 const DataChannelInit* config);
80
81 virtual void RegisterObserver(DataChannelObserver* observer);
82 virtual void UnregisterObserver();
83
wu@webrtc.org822fbd82013-08-15 23:38:54 +000084 virtual std::string label() const { return label_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 virtual bool reliable() const;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000086 virtual bool ordered() const { return config_.ordered; }
87 virtual uint16 maxRetransmitTime() const {
88 return config_.maxRetransmitTime;
89 }
90 virtual uint16 maxRetransmits() const {
91 return config_.maxRetransmits;
92 }
93 virtual std::string protocol() const { return config_.protocol; }
94 virtual bool negotiated() const { return config_.negotiated; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 virtual int id() const { return config_.id; }
96 virtual uint64 buffered_amount() const;
97 virtual void Close();
98 virtual DataState state() const { return state_; }
99 virtual bool Send(const DataBuffer& buffer);
wu@webrtc.org91053e72013-08-10 07:18:04 +0000100 // Send a control message right now, or queue for later.
101 virtual bool SendControl(const talk_base::Buffer* buffer);
102 void ConnectToDataSession();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103
104 // Set the SSRC this channel should use to receive data from the
105 // underlying data engine.
106 void SetReceiveSsrc(uint32 receive_ssrc);
107 // The remote peer request that this channel should be closed.
108 void RemotePeerRequestClose();
109
110 // Set the SSRC this channel should use to send data on the
111 // underlying data engine. |send_ssrc| == 0 means that the channel is no
112 // longer part of the session negotiation.
113 void SetSendSsrc(uint32 send_ssrc);
114
115 // Called if the underlying data engine is closing.
116 void OnDataEngineClose();
117
wu@webrtc.org91053e72013-08-10 07:18:04 +0000118 // Called when the channel's ready to use. That can happen when the
119 // underlying DataMediaChannel becomes ready, or when this channel is a new
120 // stream on an existing DataMediaChannel, and we've finished negotiation.
121 void OnChannelReady(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122
123 // Sigslots from cricket::DataChannel
124 void OnDataReceived(cricket::DataChannel* channel,
125 const cricket::ReceiveDataParams& params,
126 const talk_base::Buffer& payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127
wu@webrtc.org78187522013-10-07 23:32:02 +0000128 protected:
129 DataChannel(DataChannelProviderInterface* client,
130 cricket::DataChannelType dct,
131 const std::string& label);
132 virtual ~DataChannel();
133
134 bool Init(const DataChannelInit* config);
135 bool HasNegotiationCompleted();
136
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 private:
138 void DoClose();
139 void UpdateState();
140 void SetState(DataState state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 void DisconnectFromDataSession();
wu@webrtc.org91053e72013-08-10 07:18:04 +0000142 void DeliverQueuedControlData();
143 void QueueControl(const talk_base::Buffer* buffer);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000144 void ClearQueuedControlData();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000145 void DeliverQueuedReceivedData();
146 void ClearQueuedReceivedData();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000147 void DeliverQueuedSendData();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000148 void ClearQueuedSendData();
149 bool InternalSendWithoutQueueing(const DataBuffer& buffer,
150 cricket::SendDataResult* send_result);
151 bool QueueSendData(const DataBuffer& buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152
153 std::string label_;
154 DataChannelInit config_;
155 DataChannelObserver* observer_;
156 DataState state_;
157 bool was_ever_writable_;
wu@webrtc.org78187522013-10-07 23:32:02 +0000158 bool connected_to_provider_;
159 cricket::DataChannelType data_channel_type_;
160 DataChannelProviderInterface* provider_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 bool send_ssrc_set_;
162 uint32 send_ssrc_;
163 bool receive_ssrc_set_;
164 uint32 receive_ssrc_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000165 // Control messages that always have to get sent out before any queued
166 // data.
167 std::queue<const talk_base::Buffer*> queued_control_data_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000168 std::queue<DataBuffer*> queued_received_data_;
169 std::deque<DataBuffer*> queued_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170};
171
172class DataChannelFactory {
173 public:
174 virtual talk_base::scoped_refptr<DataChannel> CreateDataChannel(
175 const std::string& label,
176 const DataChannelInit* config) = 0;
177
178 protected:
179 virtual ~DataChannelFactory() {}
180};
181
182// Define proxy for DataChannelInterface.
183BEGIN_PROXY_MAP(DataChannel)
184 PROXY_METHOD1(void, RegisterObserver, DataChannelObserver*)
185 PROXY_METHOD0(void, UnregisterObserver)
186 PROXY_CONSTMETHOD0(std::string, label)
187 PROXY_CONSTMETHOD0(bool, reliable)
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000188 PROXY_CONSTMETHOD0(bool, ordered)
189 PROXY_CONSTMETHOD0(uint16, maxRetransmitTime)
190 PROXY_CONSTMETHOD0(uint16, maxRetransmits)
191 PROXY_CONSTMETHOD0(std::string, protocol)
192 PROXY_CONSTMETHOD0(bool, negotiated)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 PROXY_CONSTMETHOD0(int, id)
194 PROXY_CONSTMETHOD0(DataState, state)
195 PROXY_CONSTMETHOD0(uint64, buffered_amount)
196 PROXY_METHOD0(void, Close)
197 PROXY_METHOD1(bool, Send, const DataBuffer&)
198END_PROXY()
199
200} // namespace webrtc
201
202#endif // TALK_APP_WEBRTC_DATACHANNEL_H_