1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) 2012 - 2015, Daniel Stenberg, <[email protected]>, et al.
|
---|
9 | * Copyright (C) 2010, 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.haxx.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 | ***************************************************************************/
|
---|
23 |
|
---|
24 | #include "curl_setup.h"
|
---|
25 |
|
---|
26 | #ifdef USE_LIBRTMP
|
---|
27 |
|
---|
28 | #include "curl_rtmp.h"
|
---|
29 | #include "urldata.h"
|
---|
30 | #include "nonblock.h" /* for curlx_nonblock */
|
---|
31 | #include "progress.h" /* for Curl_pgrsSetUploadSize */
|
---|
32 | #include "transfer.h"
|
---|
33 | #include "warnless.h"
|
---|
34 | #include <curl/curl.h>
|
---|
35 | #include <librtmp/rtmp.h>
|
---|
36 | #include "curl_memory.h"
|
---|
37 | /* The last #include file should be: */
|
---|
38 | #include "memdebug.h"
|
---|
39 |
|
---|
40 | #if defined(WIN32) && !defined(USE_LWIPSOCK)
|
---|
41 | #define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
|
---|
42 | #define SET_RCVTIMEO(tv,s) int tv = s*1000
|
---|
43 | #elif defined(LWIP_SO_SNDRCVTIMEO_NONSTANDARD)
|
---|
44 | #define SET_RCVTIMEO(tv,s) int tv = s*1000
|
---|
45 | #else
|
---|
46 | #define SET_RCVTIMEO(tv,s) struct timeval tv = {s,0}
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | #define DEF_BUFTIME (2*60*60*1000) /* 2 hours */
|
---|
50 |
|
---|
51 | static CURLcode rtmp_setup_connection(struct connectdata *conn);
|
---|
52 | static CURLcode rtmp_do(struct connectdata *conn, bool *done);
|
---|
53 | static CURLcode rtmp_done(struct connectdata *conn, CURLcode, bool premature);
|
---|
54 | static CURLcode rtmp_connect(struct connectdata *conn, bool *done);
|
---|
55 | static CURLcode rtmp_disconnect(struct connectdata *conn, bool dead);
|
---|
56 |
|
---|
57 | static Curl_recv rtmp_recv;
|
---|
58 | static Curl_send rtmp_send;
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * RTMP protocol handler.h, based on https://rtmpdump.mplayerhq.hu
|
---|
62 | */
|
---|
63 |
|
---|
64 | const struct Curl_handler Curl_handler_rtmp = {
|
---|
65 | "RTMP", /* scheme */
|
---|
66 | rtmp_setup_connection, /* setup_connection */
|
---|
67 | rtmp_do, /* do_it */
|
---|
68 | rtmp_done, /* done */
|
---|
69 | ZERO_NULL, /* do_more */
|
---|
70 | rtmp_connect, /* connect_it */
|
---|
71 | ZERO_NULL, /* connecting */
|
---|
72 | ZERO_NULL, /* doing */
|
---|
73 | ZERO_NULL, /* proto_getsock */
|
---|
74 | ZERO_NULL, /* doing_getsock */
|
---|
75 | ZERO_NULL, /* domore_getsock */
|
---|
76 | ZERO_NULL, /* perform_getsock */
|
---|
77 | rtmp_disconnect, /* disconnect */
|
---|
78 | ZERO_NULL, /* readwrite */
|
---|
79 | ZERO_NULL, /* connection_check */
|
---|
80 | PORT_RTMP, /* defport */
|
---|
81 | CURLPROTO_RTMP, /* protocol */
|
---|
82 | PROTOPT_NONE /* flags*/
|
---|
83 | };
|
---|
84 |
|
---|
85 | const struct Curl_handler Curl_handler_rtmpt = {
|
---|
86 | "RTMPT", /* scheme */
|
---|
87 | rtmp_setup_connection, /* setup_connection */
|
---|
88 | rtmp_do, /* do_it */
|
---|
89 | rtmp_done, /* done */
|
---|
90 | ZERO_NULL, /* do_more */
|
---|
91 | rtmp_connect, /* connect_it */
|
---|
92 | ZERO_NULL, /* connecting */
|
---|
93 | ZERO_NULL, /* doing */
|
---|
94 | ZERO_NULL, /* proto_getsock */
|
---|
95 | ZERO_NULL, /* doing_getsock */
|
---|
96 | ZERO_NULL, /* domore_getsock */
|
---|
97 | ZERO_NULL, /* perform_getsock */
|
---|
98 | rtmp_disconnect, /* disconnect */
|
---|
99 | ZERO_NULL, /* readwrite */
|
---|
100 | ZERO_NULL, /* connection_check */
|
---|
101 | PORT_RTMPT, /* defport */
|
---|
102 | CURLPROTO_RTMPT, /* protocol */
|
---|
103 | PROTOPT_NONE /* flags*/
|
---|
104 | };
|
---|
105 |
|
---|
106 | const struct Curl_handler Curl_handler_rtmpe = {
|
---|
107 | "RTMPE", /* scheme */
|
---|
108 | rtmp_setup_connection, /* setup_connection */
|
---|
109 | rtmp_do, /* do_it */
|
---|
110 | rtmp_done, /* done */
|
---|
111 | ZERO_NULL, /* do_more */
|
---|
112 | rtmp_connect, /* connect_it */
|
---|
113 | ZERO_NULL, /* connecting */
|
---|
114 | ZERO_NULL, /* doing */
|
---|
115 | ZERO_NULL, /* proto_getsock */
|
---|
116 | ZERO_NULL, /* doing_getsock */
|
---|
117 | ZERO_NULL, /* domore_getsock */
|
---|
118 | ZERO_NULL, /* perform_getsock */
|
---|
119 | rtmp_disconnect, /* disconnect */
|
---|
120 | ZERO_NULL, /* readwrite */
|
---|
121 | ZERO_NULL, /* connection_check */
|
---|
122 | PORT_RTMP, /* defport */
|
---|
123 | CURLPROTO_RTMPE, /* protocol */
|
---|
124 | PROTOPT_NONE /* flags*/
|
---|
125 | };
|
---|
126 |
|
---|
127 | const struct Curl_handler Curl_handler_rtmpte = {
|
---|
128 | "RTMPTE", /* scheme */
|
---|
129 | rtmp_setup_connection, /* setup_connection */
|
---|
130 | rtmp_do, /* do_it */
|
---|
131 | rtmp_done, /* done */
|
---|
132 | ZERO_NULL, /* do_more */
|
---|
133 | rtmp_connect, /* connect_it */
|
---|
134 | ZERO_NULL, /* connecting */
|
---|
135 | ZERO_NULL, /* doing */
|
---|
136 | ZERO_NULL, /* proto_getsock */
|
---|
137 | ZERO_NULL, /* doing_getsock */
|
---|
138 | ZERO_NULL, /* domore_getsock */
|
---|
139 | ZERO_NULL, /* perform_getsock */
|
---|
140 | rtmp_disconnect, /* disconnect */
|
---|
141 | ZERO_NULL, /* readwrite */
|
---|
142 | ZERO_NULL, /* connection_check */
|
---|
143 | PORT_RTMPT, /* defport */
|
---|
144 | CURLPROTO_RTMPTE, /* protocol */
|
---|
145 | PROTOPT_NONE /* flags*/
|
---|
146 | };
|
---|
147 |
|
---|
148 | const struct Curl_handler Curl_handler_rtmps = {
|
---|
149 | "RTMPS", /* scheme */
|
---|
150 | rtmp_setup_connection, /* setup_connection */
|
---|
151 | rtmp_do, /* do_it */
|
---|
152 | rtmp_done, /* done */
|
---|
153 | ZERO_NULL, /* do_more */
|
---|
154 | rtmp_connect, /* connect_it */
|
---|
155 | ZERO_NULL, /* connecting */
|
---|
156 | ZERO_NULL, /* doing */
|
---|
157 | ZERO_NULL, /* proto_getsock */
|
---|
158 | ZERO_NULL, /* doing_getsock */
|
---|
159 | ZERO_NULL, /* domore_getsock */
|
---|
160 | ZERO_NULL, /* perform_getsock */
|
---|
161 | rtmp_disconnect, /* disconnect */
|
---|
162 | ZERO_NULL, /* readwrite */
|
---|
163 | ZERO_NULL, /* connection_check */
|
---|
164 | PORT_RTMPS, /* defport */
|
---|
165 | CURLPROTO_RTMPS, /* protocol */
|
---|
166 | PROTOPT_NONE /* flags*/
|
---|
167 | };
|
---|
168 |
|
---|
169 | const struct Curl_handler Curl_handler_rtmpts = {
|
---|
170 | "RTMPTS", /* scheme */
|
---|
171 | rtmp_setup_connection, /* setup_connection */
|
---|
172 | rtmp_do, /* do_it */
|
---|
173 | rtmp_done, /* done */
|
---|
174 | ZERO_NULL, /* do_more */
|
---|
175 | rtmp_connect, /* connect_it */
|
---|
176 | ZERO_NULL, /* connecting */
|
---|
177 | ZERO_NULL, /* doing */
|
---|
178 | ZERO_NULL, /* proto_getsock */
|
---|
179 | ZERO_NULL, /* doing_getsock */
|
---|
180 | ZERO_NULL, /* domore_getsock */
|
---|
181 | ZERO_NULL, /* perform_getsock */
|
---|
182 | rtmp_disconnect, /* disconnect */
|
---|
183 | ZERO_NULL, /* readwrite */
|
---|
184 | ZERO_NULL, /* connection_check */
|
---|
185 | PORT_RTMPS, /* defport */
|
---|
186 | CURLPROTO_RTMPTS, /* protocol */
|
---|
187 | PROTOPT_NONE /* flags*/
|
---|
188 | };
|
---|
189 |
|
---|
190 | static CURLcode rtmp_setup_connection(struct connectdata *conn)
|
---|
191 | {
|
---|
192 | RTMP *r = RTMP_Alloc();
|
---|
193 | if(!r)
|
---|
194 | return CURLE_OUT_OF_MEMORY;
|
---|
195 |
|
---|
196 | RTMP_Init(r);
|
---|
197 | RTMP_SetBufferMS(r, DEF_BUFTIME);
|
---|
198 | if(!RTMP_SetupURL(r, conn->data->change.url)) {
|
---|
199 | RTMP_Free(r);
|
---|
200 | return CURLE_URL_MALFORMAT;
|
---|
201 | }
|
---|
202 | conn->proto.generic = r;
|
---|
203 | return CURLE_OK;
|
---|
204 | }
|
---|
205 |
|
---|
206 | static CURLcode rtmp_connect(struct connectdata *conn, bool *done)
|
---|
207 | {
|
---|
208 | RTMP *r = conn->proto.generic;
|
---|
209 | SET_RCVTIMEO(tv, 10);
|
---|
210 |
|
---|
211 | r->m_sb.sb_socket = (int)conn->sock[FIRSTSOCKET];
|
---|
212 |
|
---|
213 | /* We have to know if it's a write before we send the
|
---|
214 | * connect request packet
|
---|
215 | */
|
---|
216 | if(conn->data->set.upload)
|
---|
217 | r->Link.protocol |= RTMP_FEATURE_WRITE;
|
---|
218 |
|
---|
219 | /* For plain streams, use the buffer toggle trick to keep data flowing */
|
---|
220 | if(!(r->Link.lFlags & RTMP_LF_LIVE) &&
|
---|
221 | !(r->Link.protocol & RTMP_FEATURE_HTTP))
|
---|
222 | r->Link.lFlags |= RTMP_LF_BUFX;
|
---|
223 |
|
---|
224 | (void)curlx_nonblock(r->m_sb.sb_socket, FALSE);
|
---|
225 | setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO,
|
---|
226 | (char *)&tv, sizeof(tv));
|
---|
227 |
|
---|
228 | if(!RTMP_Connect1(r, NULL))
|
---|
229 | return CURLE_FAILED_INIT;
|
---|
230 |
|
---|
231 | /* Clients must send a periodic BytesReceived report to the server */
|
---|
232 | r->m_bSendCounter = true;
|
---|
233 |
|
---|
234 | *done = TRUE;
|
---|
235 | conn->recv[FIRSTSOCKET] = rtmp_recv;
|
---|
236 | conn->send[FIRSTSOCKET] = rtmp_send;
|
---|
237 | return CURLE_OK;
|
---|
238 | }
|
---|
239 |
|
---|
240 | static CURLcode rtmp_do(struct connectdata *conn, bool *done)
|
---|
241 | {
|
---|
242 | RTMP *r = conn->proto.generic;
|
---|
243 |
|
---|
244 | if(!RTMP_ConnectStream(r, 0))
|
---|
245 | return CURLE_FAILED_INIT;
|
---|
246 |
|
---|
247 | if(conn->data->set.upload) {
|
---|
248 | Curl_pgrsSetUploadSize(conn->data, conn->data->state.infilesize);
|
---|
249 | Curl_setup_transfer(conn, -1, -1, FALSE, NULL, FIRSTSOCKET, NULL);
|
---|
250 | }
|
---|
251 | else
|
---|
252 | Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
|
---|
253 | *done = TRUE;
|
---|
254 | return CURLE_OK;
|
---|
255 | }
|
---|
256 |
|
---|
257 | static CURLcode rtmp_done(struct connectdata *conn, CURLcode status,
|
---|
258 | bool premature)
|
---|
259 | {
|
---|
260 | (void)conn; /* unused */
|
---|
261 | (void)status; /* unused */
|
---|
262 | (void)premature; /* unused */
|
---|
263 |
|
---|
264 | return CURLE_OK;
|
---|
265 | }
|
---|
266 |
|
---|
267 | static CURLcode rtmp_disconnect(struct connectdata *conn,
|
---|
268 | bool dead_connection)
|
---|
269 | {
|
---|
270 | RTMP *r = conn->proto.generic;
|
---|
271 | (void)dead_connection;
|
---|
272 | if(r) {
|
---|
273 | conn->proto.generic = NULL;
|
---|
274 | RTMP_Close(r);
|
---|
275 | RTMP_Free(r);
|
---|
276 | }
|
---|
277 | return CURLE_OK;
|
---|
278 | }
|
---|
279 |
|
---|
280 | static ssize_t rtmp_recv(struct connectdata *conn, int sockindex, char *buf,
|
---|
281 | size_t len, CURLcode *err)
|
---|
282 | {
|
---|
283 | RTMP *r = conn->proto.generic;
|
---|
284 | ssize_t nread;
|
---|
285 |
|
---|
286 | (void)sockindex; /* unused */
|
---|
287 |
|
---|
288 | nread = RTMP_Read(r, buf, curlx_uztosi(len));
|
---|
289 | if(nread < 0) {
|
---|
290 | if(r->m_read.status == RTMP_READ_COMPLETE ||
|
---|
291 | r->m_read.status == RTMP_READ_EOF) {
|
---|
292 | conn->data->req.size = conn->data->req.bytecount;
|
---|
293 | nread = 0;
|
---|
294 | }
|
---|
295 | else
|
---|
296 | *err = CURLE_RECV_ERROR;
|
---|
297 | }
|
---|
298 | return nread;
|
---|
299 | }
|
---|
300 |
|
---|
301 | static ssize_t rtmp_send(struct connectdata *conn, int sockindex,
|
---|
302 | const void *buf, size_t len, CURLcode *err)
|
---|
303 | {
|
---|
304 | RTMP *r = conn->proto.generic;
|
---|
305 | ssize_t num;
|
---|
306 |
|
---|
307 | (void)sockindex; /* unused */
|
---|
308 |
|
---|
309 | num = RTMP_Write(r, (char *)buf, curlx_uztosi(len));
|
---|
310 | if(num < 0)
|
---|
311 | *err = CURLE_SEND_ERROR;
|
---|
312 |
|
---|
313 | return num;
|
---|
314 | }
|
---|
315 | #endif /* USE_LIBRTMP */
|
---|