blob: c79c491c9cd0658316e86be8c4069653115e9aac [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
70 virtual std::string label() const { return label_; }
71 virtual bool reliable() const;
72 virtual int id() const { return config_.id; }
73 virtual uint64 buffered_amount() const;
74 virtual void Close();
75 virtual DataState state() const { return state_; }
76 virtual bool Send(const DataBuffer& buffer);
77
78 // Set the SSRC this channel should use to receive data from the
79 // underlying data engine.
80 void SetReceiveSsrc(uint32 receive_ssrc);
81 // The remote peer request that this channel should be closed.
82 void RemotePeerRequestClose();
83
84 // Set the SSRC this channel should use to send data on the
85 // underlying data engine. |send_ssrc| == 0 means that the channel is no
86 // longer part of the session negotiation.
87 void SetSendSsrc(uint32 send_ssrc);
88
89 // Called if the underlying data engine is closing.
90 void OnDataEngineClose();
91
92 protected:
93 DataChannel(WebRtcSession* session, const std::string& label);
94 virtual ~DataChannel();
95
96 bool Init(const DataChannelInit* config);
97 bool HasNegotiationCompleted();
98
99 // Sigslots from cricket::DataChannel
100 void OnDataReceived(cricket::DataChannel* channel,
101 const cricket::ReceiveDataParams& params,
102 const talk_base::Buffer& payload);
103 void OnChannelReady(bool writable);
104
105 private:
106 void DoClose();
107 void UpdateState();
108 void SetState(DataState state);
109 void ConnectToDataSession();
110 void DisconnectFromDataSession();
111 bool IsConnectedToDataSession() { return data_session_ != NULL; }
112 void DeliverQueuedData();
113 void ClearQueuedData();
114
115 std::string label_;
116 DataChannelInit config_;
117 DataChannelObserver* observer_;
118 DataState state_;
119 bool was_ever_writable_;
120 WebRtcSession* session_;
121 cricket::DataChannel* data_session_;
122 bool send_ssrc_set_;
123 uint32 send_ssrc_;
124 bool receive_ssrc_set_;
125 uint32 receive_ssrc_;
126 std::queue<DataBuffer*> queued_data_;
127};
128
129class DataChannelFactory {
130 public:
131 virtual talk_base::scoped_refptr<DataChannel> CreateDataChannel(
132 const std::string& label,
133 const DataChannelInit* config) = 0;
134
135 protected:
136 virtual ~DataChannelFactory() {}
137};
138
139// Define proxy for DataChannelInterface.
140BEGIN_PROXY_MAP(DataChannel)
141 PROXY_METHOD1(void, RegisterObserver, DataChannelObserver*)
142 PROXY_METHOD0(void, UnregisterObserver)
143 PROXY_CONSTMETHOD0(std::string, label)
144 PROXY_CONSTMETHOD0(bool, reliable)
145 PROXY_CONSTMETHOD0(int, id)
146 PROXY_CONSTMETHOD0(DataState, state)
147 PROXY_CONSTMETHOD0(uint64, buffered_amount)
148 PROXY_METHOD0(void, Close)
149 PROXY_METHOD1(bool, Send, const DataBuffer&)
150END_PROXY()
151
152} // namespace webrtc
153
154#endif // TALK_APP_WEBRTC_DATACHANNEL_H_