blob: 3ce3c1b5b4e8eb08e63b54c19d22eb9f5addaf3e [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"
38#include "talk/session/media/channel.h"
39
40namespace webrtc {
41
42class WebRtcSession;
43
44// DataChannel is a an implementation of the DataChannelInterface based on
45// libjingle's data engine. It provides an implementation of unreliable data
46// channels. Currently this class is specifically designed to use RtpDataEngine,
47// and will changed to use SCTP in the future.
48
49// DataChannel states:
50// kConnecting: The channel has been created but SSRC for sending and receiving
51// has not yet been set and the transport might not yet be ready.
52// kOpen: The channel have a local SSRC set by a call to UpdateSendSsrc
53// and a remote SSRC set by call to UpdateReceiveSsrc and the transport
54// has been writable once.
55// kClosing: DataChannelInterface::Close has been called or UpdateReceiveSsrc
56// has been called with SSRC==0
57// kClosed: Both UpdateReceiveSsrc and UpdateSendSsrc has been called with
58// SSRC==0.
59class DataChannel : public DataChannelInterface,
60 public sigslot::has_slots<> {
61 public:
62 static talk_base::scoped_refptr<DataChannel> Create(
63 WebRtcSession* session,
64 const std::string& label,
65 const DataChannelInit* config);
66
67 virtual void RegisterObserver(DataChannelObserver* observer);
68 virtual void UnregisterObserver();
69
wu@webrtc.org822fbd82013-08-15 23:38:54 +000070 virtual std::string label() const { return label_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 virtual bool reliable() const;
wu@webrtc.org822fbd82013-08-15 23:38:54 +000072 virtual bool ordered() const { return config_.ordered; }
73 virtual uint16 maxRetransmitTime() const {
74 return config_.maxRetransmitTime;
75 }
76 virtual uint16 maxRetransmits() const {
77 return config_.maxRetransmits;
78 }
79 virtual std::string protocol() const { return config_.protocol; }
80 virtual bool negotiated() const { return config_.negotiated; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 virtual int id() const { return config_.id; }
82 virtual uint64 buffered_amount() const;
83 virtual void Close();
84 virtual DataState state() const { return state_; }
85 virtual bool Send(const DataBuffer& buffer);
wu@webrtc.org91053e72013-08-10 07:18:04 +000086 // Send a control message right now, or queue for later.
87 virtual bool SendControl(const talk_base::Buffer* buffer);
88 void ConnectToDataSession();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089
90 // Set the SSRC this channel should use to receive data from the
91 // underlying data engine.
92 void SetReceiveSsrc(uint32 receive_ssrc);
93 // The remote peer request that this channel should be closed.
94 void RemotePeerRequestClose();
95
96 // Set the SSRC this channel should use to send data on the
97 // underlying data engine. |send_ssrc| == 0 means that the channel is no
98 // longer part of the session negotiation.
99 void SetSendSsrc(uint32 send_ssrc);
100
101 // Called if the underlying data engine is closing.
102 void OnDataEngineClose();
103
wu@webrtc.org91053e72013-08-10 07:18:04 +0000104 // Called when the channel's ready to use. That can happen when the
105 // underlying DataMediaChannel becomes ready, or when this channel is a new
106 // stream on an existing DataMediaChannel, and we've finished negotiation.
107 void OnChannelReady(bool writable);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 protected:
109 DataChannel(WebRtcSession* session, const std::string& label);
110 virtual ~DataChannel();
111
112 bool Init(const DataChannelInit* config);
113 bool HasNegotiationCompleted();
114
115 // Sigslots from cricket::DataChannel
116 void OnDataReceived(cricket::DataChannel* channel,
117 const cricket::ReceiveDataParams& params,
118 const talk_base::Buffer& payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119
120 private:
121 void DoClose();
122 void UpdateState();
123 void SetState(DataState state);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 void DisconnectFromDataSession();
125 bool IsConnectedToDataSession() { return data_session_ != NULL; }
wu@webrtc.org91053e72013-08-10 07:18:04 +0000126 void DeliverQueuedControlData();
127 void QueueControl(const talk_base::Buffer* buffer);
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000128 void ClearQueuedControlData();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000129 void DeliverQueuedReceivedData();
130 void ClearQueuedReceivedData();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000131 void DeliverQueuedSendData();
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000132 void ClearQueuedSendData();
133 bool InternalSendWithoutQueueing(const DataBuffer& buffer,
134 cricket::SendDataResult* send_result);
135 bool QueueSendData(const DataBuffer& buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136
137 std::string label_;
138 DataChannelInit config_;
139 DataChannelObserver* observer_;
140 DataState state_;
141 bool was_ever_writable_;
142 WebRtcSession* session_;
143 cricket::DataChannel* data_session_;
144 bool send_ssrc_set_;
145 uint32 send_ssrc_;
146 bool receive_ssrc_set_;
147 uint32 receive_ssrc_;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000148 // Control messages that always have to get sent out before any queued
149 // data.
150 std::queue<const talk_base::Buffer*> queued_control_data_;
wu@webrtc.orgd64719d2013-08-01 00:00:07 +0000151 std::queue<DataBuffer*> queued_received_data_;
152 std::deque<DataBuffer*> queued_send_data_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153};
154
155class DataChannelFactory {
156 public:
157 virtual talk_base::scoped_refptr<DataChannel> CreateDataChannel(
158 const std::string& label,
159 const DataChannelInit* config) = 0;
160
161 protected:
162 virtual ~DataChannelFactory() {}
163};
164
165// Define proxy for DataChannelInterface.
166BEGIN_PROXY_MAP(DataChannel)
167 PROXY_METHOD1(void, RegisterObserver, DataChannelObserver*)
168 PROXY_METHOD0(void, UnregisterObserver)
169 PROXY_CONSTMETHOD0(std::string, label)
170 PROXY_CONSTMETHOD0(bool, reliable)
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000171 PROXY_CONSTMETHOD0(bool, ordered)
172 PROXY_CONSTMETHOD0(uint16, maxRetransmitTime)
173 PROXY_CONSTMETHOD0(uint16, maxRetransmits)
174 PROXY_CONSTMETHOD0(std::string, protocol)
175 PROXY_CONSTMETHOD0(bool, negotiated)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 PROXY_CONSTMETHOD0(int, id)
177 PROXY_CONSTMETHOD0(DataState, state)
178 PROXY_CONSTMETHOD0(uint64, buffered_amount)
179 PROXY_METHOD0(void, Close)
180 PROXY_METHOD1(bool, Send, const DataBuffer&)
181END_PROXY()
182
183} // namespace webrtc
184
185#endif // TALK_APP_WEBRTC_DATACHANNEL_H_