blob: 97430647bf649dfab8bdad7b82be6a9c93c67afe [file] [log] [blame]
Lucas Eckels9bd90e62012-08-06 15:07:02 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07008 * Copyright (C) 2012 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
Lucas Eckels9bd90e62012-08-06 15:07:02 -07009 * Copyright (C) 2010, Howard Chu, <hyc@highlandsun.com>
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
Alex Deymo8f1a2142016-06-28 14:49:26 -070013 * are also available at https://curl.haxx.se/docs/copyright.html.
Lucas Eckels9bd90e62012-08-06 15:07:02 -070014 *
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
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070024#include "curl_setup.h"
Lucas Eckels9bd90e62012-08-06 15:07:02 -070025
26#ifdef USE_LIBRTMP
27
Elliott Hughes82be86d2017-09-20 17:00:17 -070028#include "curl_rtmp.h"
Lucas Eckels9bd90e62012-08-06 15:07:02 -070029#include "urldata.h"
30#include "nonblock.h" /* for curlx_nonblock */
31#include "progress.h" /* for Curl_pgrsSetUploadSize */
32#include "transfer.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070033#include "warnless.h"
Lucas Eckels9bd90e62012-08-06 15:07:02 -070034#include <curl/curl.h>
35#include <librtmp/rtmp.h>
Lucas Eckels9bd90e62012-08-06 15:07:02 -070036#include "curl_memory.h"
37/* The last #include file should be: */
38#include "memdebug.h"
39
40#ifdef _WIN32
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#else
44#define SET_RCVTIMEO(tv,s) struct timeval tv = {s,0}
45#endif
46
47#define DEF_BUFTIME (2*60*60*1000) /* 2 hours */
48
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070049static CURLcode rtmp_setup_connection(struct connectdata *conn);
Lucas Eckels9bd90e62012-08-06 15:07:02 -070050static CURLcode rtmp_do(struct connectdata *conn, bool *done);
51static CURLcode rtmp_done(struct connectdata *conn, CURLcode, bool premature);
52static CURLcode rtmp_connect(struct connectdata *conn, bool *done);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070053static CURLcode rtmp_disconnect(struct connectdata *conn, bool dead);
Lucas Eckels9bd90e62012-08-06 15:07:02 -070054
55static Curl_recv rtmp_recv;
56static Curl_send rtmp_send;
57
58/*
Alex Deymo8f1a2142016-06-28 14:49:26 -070059 * RTMP protocol handler.h, based on https://rtmpdump.mplayerhq.hu
Lucas Eckels9bd90e62012-08-06 15:07:02 -070060 */
61
62const struct Curl_handler Curl_handler_rtmp = {
63 "RTMP", /* scheme */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070064 rtmp_setup_connection, /* setup_connection */
Lucas Eckels9bd90e62012-08-06 15:07:02 -070065 rtmp_do, /* do_it */
66 rtmp_done, /* done */
67 ZERO_NULL, /* do_more */
68 rtmp_connect, /* connect_it */
69 ZERO_NULL, /* connecting */
70 ZERO_NULL, /* doing */
71 ZERO_NULL, /* proto_getsock */
72 ZERO_NULL, /* doing_getsock */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070073 ZERO_NULL, /* domore_getsock */
Lucas Eckels9bd90e62012-08-06 15:07:02 -070074 ZERO_NULL, /* perform_getsock */
75 rtmp_disconnect, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070076 ZERO_NULL, /* readwrite */
Elliott Hughes82be86d2017-09-20 17:00:17 -070077 ZERO_NULL, /* connection_check */
Lucas Eckels9bd90e62012-08-06 15:07:02 -070078 PORT_RTMP, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070079 CURLPROTO_RTMP, /* protocol */
80 PROTOPT_NONE /* flags*/
Lucas Eckels9bd90e62012-08-06 15:07:02 -070081};
82
83const struct Curl_handler Curl_handler_rtmpt = {
84 "RTMPT", /* scheme */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070085 rtmp_setup_connection, /* setup_connection */
Lucas Eckels9bd90e62012-08-06 15:07:02 -070086 rtmp_do, /* do_it */
87 rtmp_done, /* done */
88 ZERO_NULL, /* do_more */
89 rtmp_connect, /* connect_it */
90 ZERO_NULL, /* connecting */
91 ZERO_NULL, /* doing */
92 ZERO_NULL, /* proto_getsock */
93 ZERO_NULL, /* doing_getsock */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070094 ZERO_NULL, /* domore_getsock */
Lucas Eckels9bd90e62012-08-06 15:07:02 -070095 ZERO_NULL, /* perform_getsock */
96 rtmp_disconnect, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070097 ZERO_NULL, /* readwrite */
Elliott Hughes82be86d2017-09-20 17:00:17 -070098 ZERO_NULL, /* connection_check */
Lucas Eckels9bd90e62012-08-06 15:07:02 -070099 PORT_RTMPT, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700100 CURLPROTO_RTMPT, /* protocol */
101 PROTOPT_NONE /* flags*/
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700102};
103
104const struct Curl_handler Curl_handler_rtmpe = {
105 "RTMPE", /* scheme */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700106 rtmp_setup_connection, /* setup_connection */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700107 rtmp_do, /* do_it */
108 rtmp_done, /* done */
109 ZERO_NULL, /* do_more */
110 rtmp_connect, /* connect_it */
111 ZERO_NULL, /* connecting */
112 ZERO_NULL, /* doing */
113 ZERO_NULL, /* proto_getsock */
114 ZERO_NULL, /* doing_getsock */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700115 ZERO_NULL, /* domore_getsock */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700116 ZERO_NULL, /* perform_getsock */
117 rtmp_disconnect, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700118 ZERO_NULL, /* readwrite */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700119 ZERO_NULL, /* connection_check */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700120 PORT_RTMP, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700121 CURLPROTO_RTMPE, /* protocol */
122 PROTOPT_NONE /* flags*/
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700123};
124
125const struct Curl_handler Curl_handler_rtmpte = {
126 "RTMPTE", /* scheme */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700127 rtmp_setup_connection, /* setup_connection */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700128 rtmp_do, /* do_it */
129 rtmp_done, /* done */
130 ZERO_NULL, /* do_more */
131 rtmp_connect, /* connect_it */
132 ZERO_NULL, /* connecting */
133 ZERO_NULL, /* doing */
134 ZERO_NULL, /* proto_getsock */
135 ZERO_NULL, /* doing_getsock */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700136 ZERO_NULL, /* domore_getsock */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700137 ZERO_NULL, /* perform_getsock */
138 rtmp_disconnect, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700139 ZERO_NULL, /* readwrite */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700140 ZERO_NULL, /* connection_check */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700141 PORT_RTMPT, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700142 CURLPROTO_RTMPTE, /* protocol */
143 PROTOPT_NONE /* flags*/
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700144};
145
146const struct Curl_handler Curl_handler_rtmps = {
147 "RTMPS", /* scheme */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700148 rtmp_setup_connection, /* setup_connection */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700149 rtmp_do, /* do_it */
150 rtmp_done, /* done */
151 ZERO_NULL, /* do_more */
152 rtmp_connect, /* connect_it */
153 ZERO_NULL, /* connecting */
154 ZERO_NULL, /* doing */
155 ZERO_NULL, /* proto_getsock */
156 ZERO_NULL, /* doing_getsock */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700157 ZERO_NULL, /* domore_getsock */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700158 ZERO_NULL, /* perform_getsock */
159 rtmp_disconnect, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700160 ZERO_NULL, /* readwrite */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700161 ZERO_NULL, /* connection_check */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700162 PORT_RTMPS, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700163 CURLPROTO_RTMPS, /* protocol */
164 PROTOPT_NONE /* flags*/
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700165};
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700166
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700167const struct Curl_handler Curl_handler_rtmpts = {
168 "RTMPTS", /* scheme */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700169 rtmp_setup_connection, /* setup_connection */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700170 rtmp_do, /* do_it */
171 rtmp_done, /* done */
172 ZERO_NULL, /* do_more */
173 rtmp_connect, /* connect_it */
174 ZERO_NULL, /* connecting */
175 ZERO_NULL, /* doing */
176 ZERO_NULL, /* proto_getsock */
177 ZERO_NULL, /* doing_getsock */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700178 ZERO_NULL, /* domore_getsock */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700179 ZERO_NULL, /* perform_getsock */
180 rtmp_disconnect, /* disconnect */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700181 ZERO_NULL, /* readwrite */
Elliott Hughes82be86d2017-09-20 17:00:17 -0700182 ZERO_NULL, /* connection_check */
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700183 PORT_RTMPS, /* defport */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700184 CURLPROTO_RTMPTS, /* protocol */
185 PROTOPT_NONE /* flags*/
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700186};
187
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700188static CURLcode rtmp_setup_connection(struct connectdata *conn)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700189{
190 RTMP *r = RTMP_Alloc();
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700191 if(!r)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700192 return CURLE_OUT_OF_MEMORY;
193
194 RTMP_Init(r);
195 RTMP_SetBufferMS(r, DEF_BUFTIME);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700196 if(!RTMP_SetupURL(r, conn->data->change.url)) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700197 RTMP_Free(r);
198 return CURLE_URL_MALFORMAT;
199 }
200 conn->proto.generic = r;
201 return CURLE_OK;
202}
203
204static CURLcode rtmp_connect(struct connectdata *conn, bool *done)
205{
206 RTMP *r = conn->proto.generic;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700207 SET_RCVTIMEO(tv, 10);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700208
Elliott Hughes82be86d2017-09-20 17:00:17 -0700209 r->m_sb.sb_socket = (int)conn->sock[FIRSTSOCKET];
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700210
211 /* We have to know if it's a write before we send the
212 * connect request packet
213 */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700214 if(conn->data->set.upload)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700215 r->Link.protocol |= RTMP_FEATURE_WRITE;
216
217 /* For plain streams, use the buffer toggle trick to keep data flowing */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700218 if(!(r->Link.lFlags & RTMP_LF_LIVE) &&
219 !(r->Link.protocol & RTMP_FEATURE_HTTP))
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700220 r->Link.lFlags |= RTMP_LF_BUFX;
221
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700222 (void)curlx_nonblock(r->m_sb.sb_socket, FALSE);
223 setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO,
224 (char *)&tv, sizeof(tv));
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700225
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700226 if(!RTMP_Connect1(r, NULL))
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700227 return CURLE_FAILED_INIT;
228
229 /* Clients must send a periodic BytesReceived report to the server */
230 r->m_bSendCounter = true;
231
232 *done = TRUE;
233 conn->recv[FIRSTSOCKET] = rtmp_recv;
234 conn->send[FIRSTSOCKET] = rtmp_send;
235 return CURLE_OK;
236}
237
238static CURLcode rtmp_do(struct connectdata *conn, bool *done)
239{
240 RTMP *r = conn->proto.generic;
241
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700242 if(!RTMP_ConnectStream(r, 0))
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700243 return CURLE_FAILED_INIT;
244
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700245 if(conn->data->set.upload) {
246 Curl_pgrsSetUploadSize(conn->data, conn->data->state.infilesize);
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700247 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, FIRSTSOCKET, NULL);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700248 }
249 else
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700250 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
251 *done = TRUE;
252 return CURLE_OK;
253}
254
255static CURLcode rtmp_done(struct connectdata *conn, CURLcode status,
256 bool premature)
257{
258 (void)conn; /* unused */
259 (void)status; /* unused */
260 (void)premature; /* unused */
261
262 return CURLE_OK;
263}
264
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700265static CURLcode rtmp_disconnect(struct connectdata *conn,
266 bool dead_connection)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700267{
268 RTMP *r = conn->proto.generic;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700269 (void)dead_connection;
270 if(r) {
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700271 conn->proto.generic = NULL;
272 RTMP_Close(r);
273 RTMP_Free(r);
274 }
275 return CURLE_OK;
276}
277
278static ssize_t rtmp_recv(struct connectdata *conn, int sockindex, char *buf,
279 size_t len, CURLcode *err)
280{
281 RTMP *r = conn->proto.generic;
282 ssize_t nread;
283
284 (void)sockindex; /* unused */
285
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700286 nread = RTMP_Read(r, buf, curlx_uztosi(len));
287 if(nread < 0) {
288 if(r->m_read.status == RTMP_READ_COMPLETE ||
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700289 r->m_read.status == RTMP_READ_EOF) {
290 conn->data->req.size = conn->data->req.bytecount;
291 nread = 0;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700292 }
293 else
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700294 *err = CURLE_RECV_ERROR;
295 }
296 return nread;
297}
298
299static ssize_t rtmp_send(struct connectdata *conn, int sockindex,
300 const void *buf, size_t len, CURLcode *err)
301{
302 RTMP *r = conn->proto.generic;
303 ssize_t num;
304
305 (void)sockindex; /* unused */
306
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700307 num = RTMP_Write(r, (char *)buf, curlx_uztosi(len));
308 if(num < 0)
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700309 *err = CURLE_SEND_ERROR;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700310
Lucas Eckels9bd90e62012-08-06 15:07:02 -0700311 return num;
312}
313#endif /* USE_LIBRTMP */