blob: 9f95666452dbf7a32542df33020871d2413a1d10 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle SCTP
3 * Copyright 2012 Google Inc, and Robin Seggelmann
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_MEDIA_SCTP_SCTPDATAENGINE_H_
29#define TALK_MEDIA_SCTP_SCTPDATAENGINE_H_
30
31#include <string>
32#include <vector>
33
34#include "talk/base/buffer.h"
35#include "talk/base/scoped_ptr.h"
36#include "talk/media/base/codec.h"
37#include "talk/media/base/mediachannel.h"
38#include "talk/media/base/mediaengine.h"
39
40// Defined by "usrsctplib/usrsctp.h"
41struct sockaddr_conn;
42struct sctp_assoc_change;
43// Defined by <sys/socket.h>
44struct socket;
45
46namespace cricket {
47// A DataEngine that interacts with usrsctp.
48//
49// From channel calls, data flows like this:
50// [worker thread (although it can in princple be another thread)]
51// 1. SctpDataMediaChannel::SendData(data)
52// 2. usrsctp_sendv(data)
53// [worker thread returns; sctp thread then calls the following]
54// 3. OnSctpOutboundPacket(wrapped_data)
55// [sctp thread returns having posted a message for the worker thread]
56// 4. SctpDataMediaChannel::OnMessage(wrapped_data)
57// 5. SctpDataMediaChannel::OnPacketFromSctpToNetwork(wrapped_data)
58// 6. NetworkInterface::SendPacket(wrapped_data)
59// 7. ... across network ... a packet is sent back ...
60// 8. SctpDataMediaChannel::OnPacketReceived(wrapped_data)
61// 9. usrsctp_conninput(wrapped_data)
62// [worker thread returns; sctp thread then calls the following]
63// 10. OnSctpInboundData(data)
64// [sctp thread returns having posted a message fot the worker thread]
65// 11. SctpDataMediaChannel::OnMessage(inboundpacket)
66// 12. SctpDataMediaChannel::OnInboundPacketFromSctpToChannel(inboundpacket)
67// 13. SctpDataMediaChannel::OnDataFromSctpToChannel(data)
68// 14. SctpDataMediaChannel::SignalDataReceived(data)
69// [from the same thread, methods registered/connected to
70// SctpDataMediaChannel are called with the recieved data]
71class SctpDataEngine : public DataEngineInterface {
72 public:
73 SctpDataEngine();
74 virtual ~SctpDataEngine();
75
76 virtual DataMediaChannel* CreateChannel(DataChannelType data_channel_type);
77
78 virtual const std::vector<DataCodec>& data_codecs() { return codecs_; }
79
80 private:
81 static int usrsctp_engines_count;
82 std::vector<DataCodec> codecs_;
83};
84
85// TODO(ldixon): Make into a special type of TypedMessageData.
86// Holds data to be passed on to a channel.
87struct SctpInboundPacket;
88
89class SctpDataMediaChannel : public DataMediaChannel,
90 public talk_base::MessageHandler {
91 public:
92 // DataMessageType is used for the SCTP "Payload Protocol Identifier", as
93 // defined in http://tools.ietf.org/html/rfc4960#section-14.4
94 //
95 // For the list of IANA approved values see:
96 // http://www.iana.org/assignments/sctp-parameters/sctp-parameters.xml
97 // The value is not used by SCTP itself. It indicates the protocol running
98 // on top of SCTP.
99 enum PayloadProtocolIdentifier {
100 PPID_NONE = 0, // No protocol is specified.
101 // Specified by Mozilla. Not clear that this is actually part of the
102 // standard. Use with caution!
103 // http://mxr.mozilla.org/mozilla-central/source/netwerk/sctp/datachannel/DataChannelProtocol.h#22
104 PPID_CONTROL = 50,
105 PPID_TEXT = 51,
106 PPID_BINARY = 52,
107 };
108
109 // Given a thread which will be used to post messages (received data) to this
110 // SctpDataMediaChannel instance.
111 explicit SctpDataMediaChannel(talk_base::Thread* thread);
112 virtual ~SctpDataMediaChannel();
113
114 // When SetSend is set to true, connects. When set to false, disconnects.
115 // Calling: "SetSend(true); SetSend(false); SetSend(true);" will connect,
116 // disconnect, and reconnect.
117 virtual bool SetSend(bool send);
118 // Unless SetReceive(true) is called, received packets will be discarded.
119 virtual bool SetReceive(bool receive);
120
121 virtual bool AddSendStream(const StreamParams& sp);
122 virtual bool RemoveSendStream(uint32 ssrc);
123 virtual bool AddRecvStream(const StreamParams& sp);
124 virtual bool RemoveRecvStream(uint32 ssrc);
125
126 // Called when Sctp gets data. The data may be a notification or data for
127 // OnSctpInboundData. Called from the worker thread.
128 virtual void OnMessage(talk_base::Message* msg);
129 // Send data down this channel (will be wrapped as SCTP packets then given to
130 // sctp that will then post the network interface by OnMessage).
131 // Returns true iff successful data somewhere on the send-queue/network.
132 virtual bool SendData(const SendDataParams& params,
133 const talk_base::Buffer& payload,
134 SendDataResult* result = NULL);
135 // A packet is received from the network interface. Posted to OnMessage.
136 virtual void OnPacketReceived(talk_base::Buffer* packet);
137
138 // Exposed to allow Post call from c-callbacks.
139 talk_base::Thread* worker_thread() const { return worker_thread_; }
140
141 // TODO(ldixon): add a DataOptions class to mediachannel.h
142 virtual bool SetOptions(int options) { return false; }
143 virtual int GetOptions() const { return 0; }
144
145 // Many of these things are unused by SCTP, but are needed to fulfill
146 // the MediaChannel interface.
147 // TODO(pthatcher): Cleanup MediaChannel interface, or at least
148 // don't try calling these and return false. Right now, things
149 // don't work if we return false.
150 virtual bool SetSendBandwidth(bool autobw, int bps) { return true; }
151 virtual bool SetRecvRtpHeaderExtensions(
152 const std::vector<RtpHeaderExtension>& extensions) { return true; }
153 virtual bool SetSendRtpHeaderExtensions(
154 const std::vector<RtpHeaderExtension>& extensions) { return true; }
155 virtual bool SetSendCodecs(const std::vector<DataCodec>& codecs) {
156 return true;
157 }
158 virtual bool SetRecvCodecs(const std::vector<DataCodec>& codecs) {
159 return true;
160 }
161 virtual void OnRtcpReceived(talk_base::Buffer* packet) {}
162 virtual void OnReadyToSend(bool ready) {}
163
164 // Helper for debugging.
165 void set_debug_name(const std::string& debug_name) {
166 debug_name_ = debug_name;
167 }
168 const std::string& debug_name() const { return debug_name_; }
169
170 private:
171 sockaddr_conn GetSctpSockAddr(int port);
172
173 // Creates the socket and connects. Sets sending_ to true.
174 bool Connect();
175 // Closes the socket. Sets sending_ to false.
176 void Disconnect();
177
178 // Returns false when openning the socket failed; when successfull sets
179 // sending_ to true
180 bool OpenSctpSocket();
181 // Sets sending_ to false and sock_ to NULL.
182 void CloseSctpSocket();
183
184 // Called by OnMessage to send packet on the network.
185 void OnPacketFromSctpToNetwork(talk_base::Buffer* buffer);
186 // Called by OnMessage to decide what to do with the packet.
187 void OnInboundPacketFromSctpToChannel(SctpInboundPacket* packet);
188 void OnDataFromSctpToChannel(const ReceiveDataParams& params,
189 talk_base::Buffer* buffer);
190 void OnNotificationFromSctp(talk_base::Buffer* buffer);
191 void OnNotificationAssocChange(const sctp_assoc_change& change);
192
193 // Responsible for marshalling incoming data to the channels listeners, and
194 // outgoing data to the network interface.
195 talk_base::Thread* worker_thread_;
196 // The local and remote SCTP port to use. These are passed along the wire
197 // and the listener and connector must be using the same port. It is not
198 // related to the ports at the IP level.
199 int local_port_;
200 int remote_port_;
201 // TODO(ldixon): investigate why removing 'struct' makes the compiler
202 // complain.
203 //
204 // The socket created by usrsctp_socket(...).
205 struct socket* sock_;
206
207 // sending_ is true iff there is a connected socket.
208 bool sending_;
209 // receiving_ controls whether inbound packets are thrown away.
210 bool receiving_;
211 std::vector<StreamParams> send_streams_;
212 std::vector<StreamParams> recv_streams_;
213
214 // A human-readable name for debugging messages.
215 std::string debug_name_;
216};
217
218} // namespace cricket
219
220#endif // TALK_MEDIA_SCTP_SCTPDATAENGINE_H_