blob: 451c00d5bac62ced22c132e6292fecd85ce7e1ae [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00002 * libjingle
3 * Copyright 2012 Google Inc. and Robin Seggelmann
henrike@webrtc.org28e20752013-07-10 00:45:36 +00004 *
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
henrike@webrtc.org28654cb2013-07-22 21:07:49 +000031#include <errno.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include <string>
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000033#include <vector>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
henrike@webrtc.org28654cb2013-07-22 21:07:49 +000035namespace cricket {
36// Some ERRNO values get re-#defined to WSA* equivalents in some talk/
37// headers. We save the original ones in an enum.
38enum PreservedErrno {
39 SCTP_EINPROGRESS = EINPROGRESS,
40 SCTP_EWOULDBLOCK = EWOULDBLOCK
41};
42} // namespace cricket
43
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044#include "talk/media/base/codec.h"
45#include "talk/media/base/mediachannel.h"
46#include "talk/media/base/mediaengine.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000047#include "webrtc/base/buffer.h"
48#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049
50// Defined by "usrsctplib/usrsctp.h"
51struct sockaddr_conn;
52struct sctp_assoc_change;
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +000053struct sctp_stream_reset_event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054// Defined by <sys/socket.h>
55struct socket;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056namespace cricket {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000057// The highest stream ID (Sid) that SCTP allows, and the number of streams we
58// tell SCTP we're going to use.
wu@webrtc.org97077a32013-10-25 21:18:33 +000059const uint32 kMaxSctpSid = 1023;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000060
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +000061// This is the default SCTP port to use. It is passed along the wire and the
62// connectee and connector must be using the same port. It is not related to the
63// ports at the IP level. (Corresponds to: sockaddr_conn.sconn_port in
64// usrsctp.h)
65const int kSctpDefaultPort = 5000;
66
Lally Singhe8386d22015-08-28 14:54:37 -040067class SctpDataMediaChannel;
68
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069// A DataEngine that interacts with usrsctp.
70//
71// From channel calls, data flows like this:
72// [worker thread (although it can in princple be another thread)]
73// 1. SctpDataMediaChannel::SendData(data)
74// 2. usrsctp_sendv(data)
75// [worker thread returns; sctp thread then calls the following]
76// 3. OnSctpOutboundPacket(wrapped_data)
77// [sctp thread returns having posted a message for the worker thread]
78// 4. SctpDataMediaChannel::OnMessage(wrapped_data)
79// 5. SctpDataMediaChannel::OnPacketFromSctpToNetwork(wrapped_data)
80// 6. NetworkInterface::SendPacket(wrapped_data)
81// 7. ... across network ... a packet is sent back ...
82// 8. SctpDataMediaChannel::OnPacketReceived(wrapped_data)
83// 9. usrsctp_conninput(wrapped_data)
84// [worker thread returns; sctp thread then calls the following]
85// 10. OnSctpInboundData(data)
86// [sctp thread returns having posted a message fot the worker thread]
87// 11. SctpDataMediaChannel::OnMessage(inboundpacket)
88// 12. SctpDataMediaChannel::OnInboundPacketFromSctpToChannel(inboundpacket)
89// 13. SctpDataMediaChannel::OnDataFromSctpToChannel(data)
90// 14. SctpDataMediaChannel::SignalDataReceived(data)
91// [from the same thread, methods registered/connected to
92// SctpDataMediaChannel are called with the recieved data]
Lally Singhe8386d22015-08-28 14:54:37 -040093class SctpDataEngine : public DataEngineInterface, public sigslot::has_slots<> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 public:
95 SctpDataEngine();
96 virtual ~SctpDataEngine();
97
98 virtual DataMediaChannel* CreateChannel(DataChannelType data_channel_type);
99
100 virtual const std::vector<DataCodec>& data_codecs() { return codecs_; }
101
Lally Singhe8386d22015-08-28 14:54:37 -0400102 static int SendThresholdCallback(struct socket* sock, uint32_t sb_free);
103
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 private:
105 static int usrsctp_engines_count;
106 std::vector<DataCodec> codecs_;
Lally Singhe8386d22015-08-28 14:54:37 -0400107
108 static SctpDataMediaChannel* GetChannelFromSocket(struct socket* sock);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109};
110
111// TODO(ldixon): Make into a special type of TypedMessageData.
112// Holds data to be passed on to a channel.
113struct SctpInboundPacket;
114
115class SctpDataMediaChannel : public DataMediaChannel,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000116 public rtc::MessageHandler {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 public:
118 // DataMessageType is used for the SCTP "Payload Protocol Identifier", as
119 // defined in http://tools.ietf.org/html/rfc4960#section-14.4
120 //
121 // For the list of IANA approved values see:
122 // http://www.iana.org/assignments/sctp-parameters/sctp-parameters.xml
123 // The value is not used by SCTP itself. It indicates the protocol running
124 // on top of SCTP.
125 enum PayloadProtocolIdentifier {
126 PPID_NONE = 0, // No protocol is specified.
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000127 // Matches the PPIDs in mozilla source and
128 // https://datatracker.ietf.org/doc/draft-ietf-rtcweb-data-protocol Sec. 9
129 // They're not yet assigned by IANA.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 PPID_CONTROL = 50,
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000131 PPID_BINARY_PARTIAL = 52,
132 PPID_BINARY_LAST = 53,
133 PPID_TEXT_PARTIAL = 54,
134 PPID_TEXT_LAST = 51
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 };
136
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000137 typedef std::set<uint32> StreamSet;
138
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 // Given a thread which will be used to post messages (received data) to this
140 // SctpDataMediaChannel instance.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000141 explicit SctpDataMediaChannel(rtc::Thread* thread);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 virtual ~SctpDataMediaChannel();
143
144 // When SetSend is set to true, connects. When set to false, disconnects.
145 // Calling: "SetSend(true); SetSend(false); SetSend(true);" will connect,
146 // disconnect, and reconnect.
147 virtual bool SetSend(bool send);
148 // Unless SetReceive(true) is called, received packets will be discarded.
149 virtual bool SetReceive(bool receive);
150
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200151 virtual bool SetSendParameters(const DataSendParameters& params);
152 virtual bool SetRecvParameters(const DataRecvParameters& params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 virtual bool AddSendStream(const StreamParams& sp);
154 virtual bool RemoveSendStream(uint32 ssrc);
155 virtual bool AddRecvStream(const StreamParams& sp);
156 virtual bool RemoveRecvStream(uint32 ssrc);
157
158 // Called when Sctp gets data. The data may be a notification or data for
159 // OnSctpInboundData. Called from the worker thread.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000160 virtual void OnMessage(rtc::Message* msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 // Send data down this channel (will be wrapped as SCTP packets then given to
162 // sctp that will then post the network interface by OnMessage).
163 // Returns true iff successful data somewhere on the send-queue/network.
164 virtual bool SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000165 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 SendDataResult* result = NULL);
167 // A packet is received from the network interface. Posted to OnMessage.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000168 virtual void OnPacketReceived(rtc::Buffer* packet,
169 const rtc::PacketTime& packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170
171 // Exposed to allow Post call from c-callbacks.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000172 rtc::Thread* worker_thread() const { return worker_thread_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 // Many of these things are unused by SCTP, but are needed to fulfill
175 // the MediaChannel interface.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000176 virtual void OnRtcpReceived(rtc::Buffer* packet,
177 const rtc::PacketTime& packet_time) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 virtual void OnReadyToSend(bool ready) {}
179
Lally Singhe8386d22015-08-28 14:54:37 -0400180 void OnSendThresholdCallback();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 // Helper for debugging.
182 void set_debug_name(const std::string& debug_name) {
183 debug_name_ = debug_name;
184 }
185 const std::string& debug_name() const { return debug_name_; }
Lally Singhe8386d22015-08-28 14:54:37 -0400186 const struct socket* socket() const { return sock_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 private:
188 sockaddr_conn GetSctpSockAddr(int port);
189
Fredrik Solenbergb071a192015-09-17 16:42:56 +0200190 bool SetSendCodecs(const std::vector<DataCodec>& codecs);
191 bool SetRecvCodecs(const std::vector<DataCodec>& codecs);
192
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 // Creates the socket and connects. Sets sending_ to true.
194 bool Connect();
195 // Closes the socket. Sets sending_ to false.
196 void Disconnect();
197
198 // Returns false when openning the socket failed; when successfull sets
199 // sending_ to true
200 bool OpenSctpSocket();
201 // Sets sending_ to false and sock_ to NULL.
202 void CloseSctpSocket();
203
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000204 // Sends a SCTP_RESET_STREAM for all streams in closing_ssids_.
205 bool SendQueuedStreamResets();
206
207 // Adds a stream.
208 bool AddStream(const StreamParams &sp);
209 // Queues a stream for reset.
210 bool ResetStream(uint32 ssrc);
211
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212 // Called by OnMessage to send packet on the network.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000213 void OnPacketFromSctpToNetwork(rtc::Buffer* buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 // Called by OnMessage to decide what to do with the packet.
215 void OnInboundPacketFromSctpToChannel(SctpInboundPacket* packet);
216 void OnDataFromSctpToChannel(const ReceiveDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000217 rtc::Buffer* buffer);
218 void OnNotificationFromSctp(rtc::Buffer* buffer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 void OnNotificationAssocChange(const sctp_assoc_change& change);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000220
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000221 void OnStreamResetEvent(const struct sctp_stream_reset_event* evt);
222
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223 // Responsible for marshalling incoming data to the channels listeners, and
224 // outgoing data to the network interface.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000225 rtc::Thread* worker_thread_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 // The local and remote SCTP port to use. These are passed along the wire
227 // and the listener and connector must be using the same port. It is not
wu@webrtc.org78187522013-10-07 23:32:02 +0000228 // related to the ports at the IP level. If set to -1, we default to
229 // kSctpDefaultPort.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 int local_port_;
231 int remote_port_;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000232 struct socket* sock_; // The socket created by usrsctp_socket(...).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233
234 // sending_ is true iff there is a connected socket.
235 bool sending_;
236 // receiving_ controls whether inbound packets are thrown away.
237 bool receiving_;
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000238
239 // When a data channel opens a stream, it goes into open_streams_. When we
240 // want to close it, the stream's ID goes into queued_reset_streams_. When
241 // we actually transmit a RE-CONFIG chunk with that stream ID, the ID goes
242 // into sent_reset_streams_. When we get a response RE-CONFIG chunk back
243 // acknowledging the reset, we remove the stream ID from
244 // sent_reset_streams_. We use sent_reset_streams_ to differentiate
245 // between acknowledgment RE-CONFIG and peer-initiated RE-CONFIGs.
246 StreamSet open_streams_;
247 StreamSet queued_reset_streams_;
248 StreamSet sent_reset_streams_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249
250 // A human-readable name for debugging messages.
251 std::string debug_name_;
252};
253
254} // namespace cricket
255
256#endif // TALK_MEDIA_SCTP_SCTPDATAENGINE_H_