OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #ifndef WEBRTC_MEDIA_SCTP_SCTPDATAENGINE_H_ | |
12 #define WEBRTC_MEDIA_SCTP_SCTPDATAENGINE_H_ | |
13 | |
14 #include <errno.h> | |
15 #include <string> | |
16 #include <vector> | |
17 | |
18 namespace cricket { | |
19 // Some ERRNO values get re-#defined to WSA* equivalents in some talk/ | |
20 // headers. We save the original ones in an enum. | |
21 enum PreservedErrno { | |
22 SCTP_EINPROGRESS = EINPROGRESS, | |
23 SCTP_EWOULDBLOCK = EWOULDBLOCK | |
24 }; | |
25 } // namespace cricket | |
26 | |
27 #include "webrtc/base/copyonwritebuffer.h" | |
28 #include "webrtc/base/gtest_prod_util.h" | |
29 #include "webrtc/media/base/codec.h" | |
30 #include "webrtc/media/base/mediachannel.h" | |
31 #include "webrtc/media/base/mediaengine.h" | |
32 | |
33 // Defined by "usrsctplib/usrsctp.h" | |
34 struct sockaddr_conn; | |
35 struct sctp_assoc_change; | |
36 struct sctp_stream_reset_event; | |
37 // Defined by <sys/socket.h> | |
38 struct socket; | |
39 namespace cricket { | |
40 // The number of outgoing streams that we'll negotiate. Since stream IDs (SIDs) | |
41 // are 0-based, the highest usable SID is 1023. | |
42 // | |
43 // It's recommended to use the maximum of 65535 in: | |
44 // https://tools.ietf.org/html/draft-ietf-rtcweb-data-channel-13#section-6.2 | |
45 // However, we use 1024 in order to save memory. usrsctp allocates 104 bytes | |
46 // for each pair of incoming/outgoing streams (on a 64-bit system), so 65535 | |
47 // streams would waste ~6MB. | |
48 // | |
49 // Note: "max" and "min" here are inclusive. | |
50 constexpr uint16_t kMaxSctpStreams = 1024; | |
51 constexpr uint16_t kMaxSctpSid = kMaxSctpStreams - 1; | |
52 constexpr uint16_t kMinSctpSid = 0; | |
53 | |
54 // This is the default SCTP port to use. It is passed along the wire and the | |
55 // connectee and connector must be using the same port. It is not related to the | |
56 // ports at the IP level. (Corresponds to: sockaddr_conn.sconn_port in | |
57 // usrsctp.h) | |
58 const int kSctpDefaultPort = 5000; | |
59 | |
60 class SctpDataMediaChannel; | |
61 | |
62 // A DataEngine that interacts with usrsctp. | |
63 // | |
64 // From channel calls, data flows like this: | |
65 // [worker thread (although it can in princple be another thread)] | |
66 // 1. SctpDataMediaChannel::SendData(data) | |
67 // 2. usrsctp_sendv(data) | |
68 // [worker thread returns; sctp thread then calls the following] | |
69 // 3. OnSctpOutboundPacket(wrapped_data) | |
70 // [sctp thread returns having posted a message for the worker thread] | |
71 // 4. SctpDataMediaChannel::OnMessage(wrapped_data) | |
72 // 5. SctpDataMediaChannel::OnPacketFromSctpToNetwork(wrapped_data) | |
73 // 6. NetworkInterface::SendPacket(wrapped_data) | |
74 // 7. ... across network ... a packet is sent back ... | |
75 // 8. SctpDataMediaChannel::OnPacketReceived(wrapped_data) | |
76 // 9. usrsctp_conninput(wrapped_data) | |
77 // [worker thread returns; sctp thread then calls the following] | |
78 // 10. OnSctpInboundData(data) | |
79 // [sctp thread returns having posted a message fot the worker thread] | |
80 // 11. SctpDataMediaChannel::OnMessage(inboundpacket) | |
81 // 12. SctpDataMediaChannel::OnInboundPacketFromSctpToChannel(inboundpacket) | |
82 // 13. SctpDataMediaChannel::OnDataFromSctpToChannel(data) | |
83 // 14. SctpDataMediaChannel::SignalDataReceived(data) | |
84 // [from the same thread, methods registered/connected to | |
85 // SctpDataMediaChannel are called with the recieved data] | |
86 class SctpDataEngine : public DataEngineInterface, public sigslot::has_slots<> { | |
87 public: | |
88 SctpDataEngine(); | |
89 ~SctpDataEngine() override; | |
90 | |
91 DataMediaChannel* CreateChannel(DataChannelType data_channel_type, | |
92 const MediaConfig& config) override; | |
93 const std::vector<DataCodec>& data_codecs() override { return codecs_; } | |
94 | |
95 private: | |
96 const std::vector<DataCodec> codecs_; | |
97 }; | |
98 | |
99 // TODO(ldixon): Make into a special type of TypedMessageData. | |
100 // Holds data to be passed on to a channel. | |
101 struct SctpInboundPacket; | |
102 | |
103 class SctpDataMediaChannel : public DataMediaChannel, | |
104 public rtc::MessageHandler { | |
105 public: | |
106 // DataMessageType is used for the SCTP "Payload Protocol Identifier", as | |
107 // defined in http://tools.ietf.org/html/rfc4960#section-14.4 | |
108 // | |
109 // For the list of IANA approved values see: | |
110 // http://www.iana.org/assignments/sctp-parameters/sctp-parameters.xml | |
111 // The value is not used by SCTP itself. It indicates the protocol running | |
112 // on top of SCTP. | |
113 enum PayloadProtocolIdentifier { | |
114 PPID_NONE = 0, // No protocol is specified. | |
115 // Matches the PPIDs in mozilla source and | |
116 // https://datatracker.ietf.org/doc/draft-ietf-rtcweb-data-protocol Sec. 9 | |
117 // They're not yet assigned by IANA. | |
118 PPID_CONTROL = 50, | |
119 PPID_BINARY_PARTIAL = 52, | |
120 PPID_BINARY_LAST = 53, | |
121 PPID_TEXT_PARTIAL = 54, | |
122 PPID_TEXT_LAST = 51 | |
123 }; | |
124 | |
125 typedef std::set<uint32_t> StreamSet; | |
126 | |
127 // Given a thread which will be used to post messages (received data) to this | |
128 // SctpDataMediaChannel instance. | |
129 explicit SctpDataMediaChannel(rtc::Thread* thread, const MediaConfig& config); | |
130 virtual ~SctpDataMediaChannel(); | |
131 | |
132 // When SetSend is set to true, connects. When set to false, disconnects. | |
133 // Calling: "SetSend(true); SetSend(false); SetSend(true);" will connect, | |
134 // disconnect, and reconnect. | |
135 virtual bool SetSend(bool send); | |
136 // Unless SetReceive(true) is called, received packets will be discarded. | |
137 virtual bool SetReceive(bool receive); | |
138 | |
139 virtual bool SetSendParameters(const DataSendParameters& params); | |
140 virtual bool SetRecvParameters(const DataRecvParameters& params); | |
141 virtual bool AddSendStream(const StreamParams& sp); | |
142 virtual bool RemoveSendStream(uint32_t ssrc); | |
143 virtual bool AddRecvStream(const StreamParams& sp); | |
144 virtual bool RemoveRecvStream(uint32_t ssrc); | |
145 | |
146 // Called when Sctp gets data. The data may be a notification or data for | |
147 // OnSctpInboundData. Called from the worker thread. | |
148 virtual void OnMessage(rtc::Message* msg); | |
149 // Send data down this channel (will be wrapped as SCTP packets then given to | |
150 // sctp that will then post the network interface by OnMessage). | |
151 // Returns true iff successful data somewhere on the send-queue/network. | |
152 virtual bool SendData(const SendDataParams& params, | |
153 const rtc::CopyOnWriteBuffer& payload, | |
154 SendDataResult* result = NULL); | |
155 // A packet is received from the network interface. Posted to OnMessage. | |
156 virtual void OnPacketReceived(rtc::CopyOnWriteBuffer* packet, | |
157 const rtc::PacketTime& packet_time); | |
158 | |
159 // Exposed to allow Post call from c-callbacks. | |
160 rtc::Thread* worker_thread() const { return worker_thread_; } | |
161 | |
162 // Many of these things are unused by SCTP, but are needed to fulfill | |
163 // the MediaChannel interface. | |
164 virtual void OnRtcpReceived(rtc::CopyOnWriteBuffer* packet, | |
165 const rtc::PacketTime& packet_time) {} | |
166 virtual void OnReadyToSend(bool ready) {} | |
167 virtual void OnTransportOverheadChanged(int transport_overhead_per_packet) {} | |
168 | |
169 void OnSendThresholdCallback(); | |
170 // Helper for debugging. | |
171 void set_debug_name_for_testing(const char* debug_name) { | |
172 debug_name_ = debug_name; | |
173 } | |
174 const struct socket* socket() const { return sock_; } | |
175 | |
176 private: | |
177 FRIEND_TEST_ALL_PREFIXES(SctpDataMediaChannelTest, EngineSignalsRightChannel); | |
178 static int SendThresholdCallback(struct socket* sock, uint32_t sb_free); | |
179 static SctpDataMediaChannel* GetChannelFromSocket(struct socket* sock); | |
180 | |
181 private: | |
182 sockaddr_conn GetSctpSockAddr(int port); | |
183 | |
184 bool SetSendCodecs(const std::vector<DataCodec>& codecs); | |
185 bool SetRecvCodecs(const std::vector<DataCodec>& codecs); | |
186 | |
187 // Creates the socket and connects. Sets sending_ to true. | |
188 bool Connect(); | |
189 // Closes the socket. Sets sending_ to false. | |
190 void Disconnect(); | |
191 | |
192 // Returns false when openning the socket failed; when successfull sets | |
193 // sending_ to true | |
194 bool OpenSctpSocket(); | |
195 // Sets sending_ to false and sock_ to NULL. | |
196 void CloseSctpSocket(); | |
197 | |
198 // Sends a SCTP_RESET_STREAM for all streams in closing_ssids_. | |
199 bool SendQueuedStreamResets(); | |
200 | |
201 // Adds a stream. | |
202 bool AddStream(const StreamParams &sp); | |
203 // Queues a stream for reset. | |
204 bool ResetStream(uint32_t ssrc); | |
205 | |
206 // Called by OnMessage to send packet on the network. | |
207 void OnPacketFromSctpToNetwork(rtc::CopyOnWriteBuffer* buffer); | |
208 // Called by OnMessage to decide what to do with the packet. | |
209 void OnInboundPacketFromSctpToChannel(SctpInboundPacket* packet); | |
210 void OnDataFromSctpToChannel(const ReceiveDataParams& params, | |
211 const rtc::CopyOnWriteBuffer& buffer); | |
212 void OnNotificationFromSctp(const rtc::CopyOnWriteBuffer& buffer); | |
213 void OnNotificationAssocChange(const sctp_assoc_change& change); | |
214 | |
215 void OnStreamResetEvent(const struct sctp_stream_reset_event* evt); | |
216 | |
217 // Responsible for marshalling incoming data to the channels listeners, and | |
218 // outgoing data to the network interface. | |
219 rtc::Thread* worker_thread_; | |
220 // The local and remote SCTP port to use. These are passed along the wire | |
221 // and the listener and connector must be using the same port. It is not | |
222 // related to the ports at the IP level. If set to -1, we default to | |
223 // kSctpDefaultPort. | |
224 int local_port_; | |
225 int remote_port_; | |
226 struct socket* sock_; // The socket created by usrsctp_socket(...). | |
227 | |
228 // sending_ is true iff there is a connected socket. | |
229 bool sending_; | |
230 // receiving_ controls whether inbound packets are thrown away. | |
231 bool receiving_; | |
232 | |
233 // When a data channel opens a stream, it goes into open_streams_. When we | |
234 // want to close it, the stream's ID goes into queued_reset_streams_. When | |
235 // we actually transmit a RE-CONFIG chunk with that stream ID, the ID goes | |
236 // into sent_reset_streams_. When we get a response RE-CONFIG chunk back | |
237 // acknowledging the reset, we remove the stream ID from | |
238 // sent_reset_streams_. We use sent_reset_streams_ to differentiate | |
239 // between acknowledgment RE-CONFIG and peer-initiated RE-CONFIGs. | |
240 StreamSet open_streams_; | |
241 StreamSet queued_reset_streams_; | |
242 StreamSet sent_reset_streams_; | |
243 | |
244 // A static human-readable name for debugging messages. | |
245 const char* debug_name_; | |
246 }; | |
247 | |
248 } // namespace cricket | |
249 | |
250 #endif // WEBRTC_MEDIA_SCTP_SCTPDATAENGINE_H_ | |
OLD | NEW |