1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
|
---|
9 | * Copyright (C) Howard Chu, <[email protected]>
|
---|
10 | *
|
---|
11 | * This software is licensed as described in the file COPYING, which
|
---|
12 | * you should have received as part of this distribution. The terms
|
---|
13 | * are also available at https://curl.se/docs/copyright.html.
|
---|
14 | *
|
---|
15 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
---|
16 | * copies of the Software, and permit persons to whom the Software is
|
---|
17 | * furnished to do so, under the terms of the COPYING file.
|
---|
18 | *
|
---|
19 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
---|
20 | * KIND, either express or implied.
|
---|
21 | *
|
---|
22 | * SPDX-License-Identifier: curl
|
---|
23 | *
|
---|
24 | ***************************************************************************/
|
---|
25 |
|
---|
26 | #include "curl_setup.h"
|
---|
27 |
|
---|
28 | #ifdef USE_LIBRTMP
|
---|
29 |
|
---|
30 | #include "curl_rtmp.h"
|
---|
31 | #include "urldata.h"
|
---|
32 | #include "nonblock.h" /* for curlx_nonblock */
|
---|
33 | #include "progress.h" /* for Curl_pgrsSetUploadSize */
|
---|
34 | #include "transfer.h"
|
---|
35 | #include "warnless.h"
|
---|
36 | #include <curl/curl.h>
|
---|
37 | #include <librtmp/rtmp.h>
|
---|
38 |
|
---|
39 | /* The last 3 #include files should be in this order */
|
---|
40 | #include "curl_printf.h"
|
---|
41 | #include "curl_memory.h"
|
---|
42 | #include "memdebug.h"
|
---|
43 |
|
---|
44 | #if defined(_WIN32) && !defined(USE_LWIPSOCK)
|
---|
45 | #define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
|
---|
46 | #define SET_RCVTIMEO(tv,s) int tv = s*1000
|
---|
47 | #elif defined(LWIP_SO_SNDRCVTIMEO_NONSTANDARD)
|
---|
48 | #define SET_RCVTIMEO(tv,s) int tv = s*1000
|
---|
49 | #else
|
---|
50 | #define SET_RCVTIMEO(tv,s) struct timeval tv = {s,0}
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #define DEF_BUFTIME (2*60*60*1000) /* 2 hours */
|
---|
54 |
|
---|
55 | static CURLcode rtmp_setup_connection(struct Curl_easy *data,
|
---|
56 | struct connectdata *conn);
|
---|
57 | static CURLcode rtmp_do(struct Curl_easy *data, bool *done);
|
---|
58 | static CURLcode rtmp_done(struct Curl_easy *data, CURLcode, bool premature);
|
---|
59 | static CURLcode rtmp_connect(struct Curl_easy *data, bool *done);
|
---|
60 | static CURLcode rtmp_disconnect(struct Curl_easy *data,
|
---|
61 | struct connectdata *conn, bool dead);
|
---|
62 |
|
---|
63 | static Curl_recv rtmp_recv;
|
---|
64 | static Curl_send rtmp_send;
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * RTMP protocol handler.h, based on https://rtmpdump.mplayerhq.hu
|
---|
68 | */
|
---|
69 |
|
---|
70 | const struct Curl_handler Curl_handler_rtmp = {
|
---|
71 | "rtmp", /* scheme */
|
---|
72 | rtmp_setup_connection, /* setup_connection */
|
---|
73 | rtmp_do, /* do_it */
|
---|
74 | rtmp_done, /* done */
|
---|
75 | ZERO_NULL, /* do_more */
|
---|
76 | rtmp_connect, /* connect_it */
|
---|
77 | ZERO_NULL, /* connecting */
|
---|
78 | ZERO_NULL, /* doing */
|
---|
79 | ZERO_NULL, /* proto_getsock */
|
---|
80 | ZERO_NULL, /* doing_getsock */
|
---|
81 | ZERO_NULL, /* domore_getsock */
|
---|
82 | ZERO_NULL, /* perform_getsock */
|
---|
83 | rtmp_disconnect, /* disconnect */
|
---|
84 | ZERO_NULL, /* write_resp */
|
---|
85 | ZERO_NULL, /* write_resp_hd */
|
---|
86 | ZERO_NULL, /* connection_check */
|
---|
87 | ZERO_NULL, /* attach connection */
|
---|
88 | PORT_RTMP, /* defport */
|
---|
89 | CURLPROTO_RTMP, /* protocol */
|
---|
90 | CURLPROTO_RTMP, /* family */
|
---|
91 | PROTOPT_NONE /* flags */
|
---|
92 | };
|
---|
93 |
|
---|
94 | const struct Curl_handler Curl_handler_rtmpt = {
|
---|
95 | "rtmpt", /* scheme */
|
---|
96 | rtmp_setup_connection, /* setup_connection */
|
---|
97 | rtmp_do, /* do_it */
|
---|
98 | rtmp_done, /* done */
|
---|
99 | ZERO_NULL, /* do_more */
|
---|
100 | rtmp_connect, /* connect_it */
|
---|
101 | ZERO_NULL, /* connecting */
|
---|
102 | ZERO_NULL, /* doing */
|
---|
103 | ZERO_NULL, /* proto_getsock */
|
---|
104 | ZERO_NULL, /* doing_getsock */
|
---|
105 | ZERO_NULL, /* domore_getsock */
|
---|
106 | ZERO_NULL, /* perform_getsock */
|
---|
107 | rtmp_disconnect, /* disconnect */
|
---|
108 | ZERO_NULL, /* write_resp */
|
---|
109 | ZERO_NULL, /* write_resp_hd */
|
---|
110 | ZERO_NULL, /* connection_check */
|
---|
111 | ZERO_NULL, /* attach connection */
|
---|
112 | PORT_RTMPT, /* defport */
|
---|
113 | CURLPROTO_RTMPT, /* protocol */
|
---|
114 | CURLPROTO_RTMPT, /* family */
|
---|
115 | PROTOPT_NONE /* flags */
|
---|
116 | };
|
---|
117 |
|
---|
118 | const struct Curl_handler Curl_handler_rtmpe = {
|
---|
119 | "rtmpe", /* scheme */
|
---|
120 | rtmp_setup_connection, /* setup_connection */
|
---|
121 | rtmp_do, /* do_it */
|
---|
122 | rtmp_done, /* done */
|
---|
123 | ZERO_NULL, /* do_more */
|
---|
124 | rtmp_connect, /* connect_it */
|
---|
125 | ZERO_NULL, /* connecting */
|
---|
126 | ZERO_NULL, /* doing */
|
---|
127 | ZERO_NULL, /* proto_getsock */
|
---|
128 | ZERO_NULL, /* doing_getsock */
|
---|
129 | ZERO_NULL, /* domore_getsock */
|
---|
130 | ZERO_NULL, /* perform_getsock */
|
---|
131 | rtmp_disconnect, /* disconnect */
|
---|
132 | ZERO_NULL, /* write_resp */
|
---|
133 | ZERO_NULL, /* write_resp_hd */
|
---|
134 | ZERO_NULL, /* connection_check */
|
---|
135 | ZERO_NULL, /* attach connection */
|
---|
136 | PORT_RTMP, /* defport */
|
---|
137 | CURLPROTO_RTMPE, /* protocol */
|
---|
138 | CURLPROTO_RTMPE, /* family */
|
---|
139 | PROTOPT_NONE /* flags */
|
---|
140 | };
|
---|
141 |
|
---|
142 | const struct Curl_handler Curl_handler_rtmpte = {
|
---|
143 | "rtmpte", /* scheme */
|
---|
144 | rtmp_setup_connection, /* setup_connection */
|
---|
145 | rtmp_do, /* do_it */
|
---|
146 | rtmp_done, /* done */
|
---|
147 | ZERO_NULL, /* do_more */
|
---|
148 | rtmp_connect, /* connect_it */
|
---|
149 | ZERO_NULL, /* connecting */
|
---|
150 | ZERO_NULL, /* doing */
|
---|
151 | ZERO_NULL, /* proto_getsock */
|
---|
152 | ZERO_NULL, /* doing_getsock */
|
---|
153 | ZERO_NULL, /* domore_getsock */
|
---|
154 | ZERO_NULL, /* perform_getsock */
|
---|
155 | rtmp_disconnect, /* disconnect */
|
---|
156 | ZERO_NULL, /* write_resp */
|
---|
157 | ZERO_NULL, /* write_resp_hd */
|
---|
158 | ZERO_NULL, /* connection_check */
|
---|
159 | ZERO_NULL, /* attach connection */
|
---|
160 | PORT_RTMPT, /* defport */
|
---|
161 | CURLPROTO_RTMPTE, /* protocol */
|
---|
162 | CURLPROTO_RTMPTE, /* family */
|
---|
163 | PROTOPT_NONE /* flags */
|
---|
164 | };
|
---|
165 |
|
---|
166 | const struct Curl_handler Curl_handler_rtmps = {
|
---|
167 | "rtmps", /* scheme */
|
---|
168 | rtmp_setup_connection, /* setup_connection */
|
---|
169 | rtmp_do, /* do_it */
|
---|
170 | rtmp_done, /* done */
|
---|
171 | ZERO_NULL, /* do_more */
|
---|
172 | rtmp_connect, /* connect_it */
|
---|
173 | ZERO_NULL, /* connecting */
|
---|
174 | ZERO_NULL, /* doing */
|
---|
175 | ZERO_NULL, /* proto_getsock */
|
---|
176 | ZERO_NULL, /* doing_getsock */
|
---|
177 | ZERO_NULL, /* domore_getsock */
|
---|
178 | ZERO_NULL, /* perform_getsock */
|
---|
179 | rtmp_disconnect, /* disconnect */
|
---|
180 | ZERO_NULL, /* write_resp */
|
---|
181 | ZERO_NULL, /* write_resp_hd */
|
---|
182 | ZERO_NULL, /* connection_check */
|
---|
183 | ZERO_NULL, /* attach connection */
|
---|
184 | PORT_RTMPS, /* defport */
|
---|
185 | CURLPROTO_RTMPS, /* protocol */
|
---|
186 | CURLPROTO_RTMP, /* family */
|
---|
187 | PROTOPT_NONE /* flags */
|
---|
188 | };
|
---|
189 |
|
---|
190 | const struct Curl_handler Curl_handler_rtmpts = {
|
---|
191 | "rtmpts", /* scheme */
|
---|
192 | rtmp_setup_connection, /* setup_connection */
|
---|
193 | rtmp_do, /* do_it */
|
---|
194 | rtmp_done, /* done */
|
---|
195 | ZERO_NULL, /* do_more */
|
---|
196 | rtmp_connect, /* connect_it */
|
---|
197 | ZERO_NULL, /* connecting */
|
---|
198 | ZERO_NULL, /* doing */
|
---|
199 | ZERO_NULL, /* proto_getsock */
|
---|
200 | ZERO_NULL, /* doing_getsock */
|
---|
201 | ZERO_NULL, /* domore_getsock */
|
---|
202 | ZERO_NULL, /* perform_getsock */
|
---|
203 | rtmp_disconnect, /* disconnect */
|
---|
204 | ZERO_NULL, /* write_resp */
|
---|
205 | ZERO_NULL, /* write_resp_hd */
|
---|
206 | ZERO_NULL, /* connection_check */
|
---|
207 | ZERO_NULL, /* attach connection */
|
---|
208 | PORT_RTMPS, /* defport */
|
---|
209 | CURLPROTO_RTMPTS, /* protocol */
|
---|
210 | CURLPROTO_RTMPT, /* family */
|
---|
211 | PROTOPT_NONE /* flags */
|
---|
212 | };
|
---|
213 |
|
---|
214 | static CURLcode rtmp_setup_connection(struct Curl_easy *data,
|
---|
215 | struct connectdata *conn)
|
---|
216 | {
|
---|
217 | RTMP *r = RTMP_Alloc();
|
---|
218 | if(!r)
|
---|
219 | return CURLE_OUT_OF_MEMORY;
|
---|
220 |
|
---|
221 | RTMP_Init(r);
|
---|
222 | RTMP_SetBufferMS(r, DEF_BUFTIME);
|
---|
223 | if(!RTMP_SetupURL(r, data->state.url)) {
|
---|
224 | RTMP_Free(r);
|
---|
225 | return CURLE_URL_MALFORMAT;
|
---|
226 | }
|
---|
227 | conn->proto.rtmp = r;
|
---|
228 | return CURLE_OK;
|
---|
229 | }
|
---|
230 |
|
---|
231 | static CURLcode rtmp_connect(struct Curl_easy *data, bool *done)
|
---|
232 | {
|
---|
233 | struct connectdata *conn = data->conn;
|
---|
234 | RTMP *r = conn->proto.rtmp;
|
---|
235 | SET_RCVTIMEO(tv, 10);
|
---|
236 |
|
---|
237 | r->m_sb.sb_socket = (int)conn->sock[FIRSTSOCKET];
|
---|
238 |
|
---|
239 | /* We have to know if it is a write before we send the
|
---|
240 | * connect request packet
|
---|
241 | */
|
---|
242 | if(data->state.upload)
|
---|
243 | r->Link.protocol |= RTMP_FEATURE_WRITE;
|
---|
244 |
|
---|
245 | /* For plain streams, use the buffer toggle trick to keep data flowing */
|
---|
246 | if(!(r->Link.lFlags & RTMP_LF_LIVE) &&
|
---|
247 | !(r->Link.protocol & RTMP_FEATURE_HTTP))
|
---|
248 | r->Link.lFlags |= RTMP_LF_BUFX;
|
---|
249 |
|
---|
250 | (void)curlx_nonblock(r->m_sb.sb_socket, FALSE);
|
---|
251 | setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO,
|
---|
252 | (char *)&tv, sizeof(tv));
|
---|
253 |
|
---|
254 | if(!RTMP_Connect1(r, NULL))
|
---|
255 | return CURLE_FAILED_INIT;
|
---|
256 |
|
---|
257 | /* Clients must send a periodic BytesReceived report to the server */
|
---|
258 | r->m_bSendCounter = TRUE;
|
---|
259 |
|
---|
260 | *done = TRUE;
|
---|
261 | conn->recv[FIRSTSOCKET] = rtmp_recv;
|
---|
262 | conn->send[FIRSTSOCKET] = rtmp_send;
|
---|
263 | return CURLE_OK;
|
---|
264 | }
|
---|
265 |
|
---|
266 | static CURLcode rtmp_do(struct Curl_easy *data, bool *done)
|
---|
267 | {
|
---|
268 | struct connectdata *conn = data->conn;
|
---|
269 | RTMP *r = conn->proto.rtmp;
|
---|
270 |
|
---|
271 | if(!RTMP_ConnectStream(r, 0))
|
---|
272 | return CURLE_FAILED_INIT;
|
---|
273 |
|
---|
274 | if(data->state.upload) {
|
---|
275 | Curl_pgrsSetUploadSize(data, data->state.infilesize);
|
---|
276 | Curl_xfer_setup1(data, CURL_XFER_SEND, -1, FALSE);
|
---|
277 | }
|
---|
278 | else
|
---|
279 | Curl_xfer_setup1(data, CURL_XFER_RECV, -1, FALSE);
|
---|
280 | *done = TRUE;
|
---|
281 | return CURLE_OK;
|
---|
282 | }
|
---|
283 |
|
---|
284 | static CURLcode rtmp_done(struct Curl_easy *data, CURLcode status,
|
---|
285 | bool premature)
|
---|
286 | {
|
---|
287 | (void)data; /* unused */
|
---|
288 | (void)status; /* unused */
|
---|
289 | (void)premature; /* unused */
|
---|
290 |
|
---|
291 | return CURLE_OK;
|
---|
292 | }
|
---|
293 |
|
---|
294 | static CURLcode rtmp_disconnect(struct Curl_easy *data,
|
---|
295 | struct connectdata *conn,
|
---|
296 | bool dead_connection)
|
---|
297 | {
|
---|
298 | RTMP *r = conn->proto.rtmp;
|
---|
299 | (void)data;
|
---|
300 | (void)dead_connection;
|
---|
301 | if(r) {
|
---|
302 | conn->proto.rtmp = NULL;
|
---|
303 | RTMP_Close(r);
|
---|
304 | RTMP_Free(r);
|
---|
305 | }
|
---|
306 | return CURLE_OK;
|
---|
307 | }
|
---|
308 |
|
---|
309 | static ssize_t rtmp_recv(struct Curl_easy *data, int sockindex, char *buf,
|
---|
310 | size_t len, CURLcode *err)
|
---|
311 | {
|
---|
312 | struct connectdata *conn = data->conn;
|
---|
313 | RTMP *r = conn->proto.rtmp;
|
---|
314 | ssize_t nread;
|
---|
315 |
|
---|
316 | (void)sockindex; /* unused */
|
---|
317 |
|
---|
318 | nread = RTMP_Read(r, buf, curlx_uztosi(len));
|
---|
319 | if(nread < 0) {
|
---|
320 | if(r->m_read.status == RTMP_READ_COMPLETE ||
|
---|
321 | r->m_read.status == RTMP_READ_EOF) {
|
---|
322 | data->req.size = data->req.bytecount;
|
---|
323 | nread = 0;
|
---|
324 | }
|
---|
325 | else
|
---|
326 | *err = CURLE_RECV_ERROR;
|
---|
327 | }
|
---|
328 | return nread;
|
---|
329 | }
|
---|
330 |
|
---|
331 | static ssize_t rtmp_send(struct Curl_easy *data, int sockindex,
|
---|
332 | const void *buf, size_t len, bool eos, CURLcode *err)
|
---|
333 | {
|
---|
334 | struct connectdata *conn = data->conn;
|
---|
335 | RTMP *r = conn->proto.rtmp;
|
---|
336 | ssize_t num;
|
---|
337 |
|
---|
338 | (void)sockindex; /* unused */
|
---|
339 | (void)eos; /* unused */
|
---|
340 |
|
---|
341 | num = RTMP_Write(r, (char *)buf, curlx_uztosi(len));
|
---|
342 | if(num < 0)
|
---|
343 | *err = CURLE_SEND_ERROR;
|
---|
344 |
|
---|
345 | return num;
|
---|
346 | }
|
---|
347 |
|
---|
348 | void Curl_rtmp_version(char *version, size_t len)
|
---|
349 | {
|
---|
350 | char suff[2];
|
---|
351 | if(RTMP_LIB_VERSION & 0xff) {
|
---|
352 | suff[0] = (RTMP_LIB_VERSION & 0xff) + 'a' - 1;
|
---|
353 | suff[1] = '\0';
|
---|
354 | }
|
---|
355 | else
|
---|
356 | suff[0] = '\0';
|
---|
357 |
|
---|
358 | msnprintf(version, len, "librtmp/%d.%d%s",
|
---|
359 | RTMP_LIB_VERSION >> 16, (RTMP_LIB_VERSION >> 8) & 0xff,
|
---|
360 | suff);
|
---|
361 | }
|
---|
362 |
|
---|
363 | #endif /* USE_LIBRTMP */
|
---|