Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 311 lines (270 sloc) 11.264 kb
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2010, Howard Chu, <hyc@highlandsun.com>
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
23 #include "setup.h"
24
25 #ifdef USE_LIBRTMP
26
27 #include "urldata.h"
d3c813e @jchaffraix RTMP: Fix compiler warnings
jchaffraix authored
28 #include "nonblock.h" /* for curlx_nonblock */
29 #include "progress.h" /* for Curl_pgrsSetUploadSize */
30 #include "transfer.h"
dee2ef8 @mback2k krb5/curl_rtmp.c: Hide size_t to int type conversion warning
mback2k authored
31 #include "warnless.h"
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
32 #include <curl/curl.h>
33 #include <librtmp/rtmp.h>
34
077125e @yangtse include libcurl standard internal headers
yangtse authored
35 #define _MPRINTF_REPLACE /* use our functions only */
36 #include <curl/mprintf.h>
37
38 #include "curl_memory.h"
39 /* The last #include file should be: */
40 #include "memdebug.h"
41
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
42 #ifdef _WIN32
43 #define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
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(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);
b903186 @bagder source cleanup: unify look, style and indent levels
authored
55 static CURLcode rtmp_disconnect(struct connectdata *conn, bool dead);
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
56
57 static Curl_recv rtmp_recv;
58 static Curl_send rtmp_send;
59
60 /*
61 * RTMP protocol handler.h, based on http://rtmpdump.mplayerhq.hu
62 */
63
64 const struct Curl_handler Curl_handler_rtmp = {
65 "RTMP", /* scheme */
66 rtmp_setup, /* 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 */
d7934b8 @bagder curl_multi_fdset: correct fdset with FTP PORT use
authored
75 ZERO_NULL, /* domore_getsock */
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
76 ZERO_NULL, /* perform_getsock */
77 rtmp_disconnect, /* disconnect */
f0612f1 @bagder RTSP: convert protocol-specific checks to generic
authored
78 ZERO_NULL, /* readwrite */
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
79 PORT_RTMP, /* defport */
13b64d7 @bagder protocols: use CURLPROTO_ internally
authored
80 CURLPROTO_RTMP, /* protocol */
8831000 @bagder protocol handler: added flags field
authored
81 PROTOPT_NONE /* flags*/
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
82 };
83
84 const struct Curl_handler Curl_handler_rtmpt = {
85 "RTMPT", /* scheme */
86 rtmp_setup, /* setup_connection */
87 rtmp_do, /* do_it */
88 rtmp_done, /* done */
89 ZERO_NULL, /* do_more */
90 rtmp_connect, /* connect_it */
91 ZERO_NULL, /* connecting */
92 ZERO_NULL, /* doing */
93 ZERO_NULL, /* proto_getsock */
94 ZERO_NULL, /* doing_getsock */
d7934b8 @bagder curl_multi_fdset: correct fdset with FTP PORT use
authored
95 ZERO_NULL, /* domore_getsock */
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
96 ZERO_NULL, /* perform_getsock */
97 rtmp_disconnect, /* disconnect */
f0612f1 @bagder RTSP: convert protocol-specific checks to generic
authored
98 ZERO_NULL, /* readwrite */
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
99 PORT_RTMPT, /* defport */
13b64d7 @bagder protocols: use CURLPROTO_ internally
authored
100 CURLPROTO_RTMPT, /* protocol */
8831000 @bagder protocol handler: added flags field
authored
101 PROTOPT_NONE /* flags*/
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
102 };
103
104 const struct Curl_handler Curl_handler_rtmpe = {
105 "RTMPE", /* scheme */
106 rtmp_setup, /* setup_connection */
107 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 */
d7934b8 @bagder curl_multi_fdset: correct fdset with FTP PORT use
authored
115 ZERO_NULL, /* domore_getsock */
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
116 ZERO_NULL, /* perform_getsock */
117 rtmp_disconnect, /* disconnect */
f0612f1 @bagder RTSP: convert protocol-specific checks to generic
authored
118 ZERO_NULL, /* readwrite */
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
119 PORT_RTMP, /* defport */
13b64d7 @bagder protocols: use CURLPROTO_ internally
authored
120 CURLPROTO_RTMPE, /* protocol */
8831000 @bagder protocol handler: added flags field
authored
121 PROTOPT_NONE /* flags*/
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
122 };
123
124 const struct Curl_handler Curl_handler_rtmpte = {
125 "RTMPTE", /* scheme */
126 rtmp_setup, /* setup_connection */
127 rtmp_do, /* do_it */
128 rtmp_done, /* done */
129 ZERO_NULL, /* do_more */
130 rtmp_connect, /* connect_it */
131 ZERO_NULL, /* connecting */
132 ZERO_NULL, /* doing */
133 ZERO_NULL, /* proto_getsock */
134 ZERO_NULL, /* doing_getsock */
d7934b8 @bagder curl_multi_fdset: correct fdset with FTP PORT use
authored
135 ZERO_NULL, /* domore_getsock */
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
136 ZERO_NULL, /* perform_getsock */
137 rtmp_disconnect, /* disconnect */
f0612f1 @bagder RTSP: convert protocol-specific checks to generic
authored
138 ZERO_NULL, /* readwrite */
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
139 PORT_RTMPT, /* defport */
13b64d7 @bagder protocols: use CURLPROTO_ internally
authored
140 CURLPROTO_RTMPTE, /* protocol */
8831000 @bagder protocol handler: added flags field
authored
141 PROTOPT_NONE /* flags*/
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
142 };
143
144 const struct Curl_handler Curl_handler_rtmps = {
145 "RTMPS", /* scheme */
146 rtmp_setup, /* setup_connection */
147 rtmp_do, /* do_it */
148 rtmp_done, /* done */
149 ZERO_NULL, /* do_more */
150 rtmp_connect, /* connect_it */
151 ZERO_NULL, /* connecting */
152 ZERO_NULL, /* doing */
153 ZERO_NULL, /* proto_getsock */
154 ZERO_NULL, /* doing_getsock */
d7934b8 @bagder curl_multi_fdset: correct fdset with FTP PORT use
authored
155 ZERO_NULL, /* domore_getsock */
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
156 ZERO_NULL, /* perform_getsock */
157 rtmp_disconnect, /* disconnect */
f0612f1 @bagder RTSP: convert protocol-specific checks to generic
authored
158 ZERO_NULL, /* readwrite */
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
159 PORT_RTMPS, /* defport */
13b64d7 @bagder protocols: use CURLPROTO_ internally
authored
160 CURLPROTO_RTMPS, /* protocol */
8831000 @bagder protocol handler: added flags field
authored
161 PROTOPT_NONE /* flags*/
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
162 };
f0612f1 @bagder RTSP: convert protocol-specific checks to generic
authored
163
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
164 const struct Curl_handler Curl_handler_rtmpts = {
165 "RTMPTS", /* scheme */
166 rtmp_setup, /* setup_connection */
167 rtmp_do, /* do_it */
168 rtmp_done, /* done */
169 ZERO_NULL, /* do_more */
170 rtmp_connect, /* connect_it */
171 ZERO_NULL, /* connecting */
172 ZERO_NULL, /* doing */
173 ZERO_NULL, /* proto_getsock */
174 ZERO_NULL, /* doing_getsock */
d7934b8 @bagder curl_multi_fdset: correct fdset with FTP PORT use
authored
175 ZERO_NULL, /* domore_getsock */
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
176 ZERO_NULL, /* perform_getsock */
177 rtmp_disconnect, /* disconnect */
f0612f1 @bagder RTSP: convert protocol-specific checks to generic
authored
178 ZERO_NULL, /* readwrite */
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
179 PORT_RTMPS, /* defport */
13b64d7 @bagder protocols: use CURLPROTO_ internally
authored
180 CURLPROTO_RTMPTS, /* protocol */
8831000 @bagder protocol handler: added flags field
authored
181 PROTOPT_NONE /* flags*/
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
182 };
183
184 static CURLcode rtmp_setup(struct connectdata *conn)
185 {
186 RTMP *r = RTMP_Alloc();
187
b903186 @bagder source cleanup: unify look, style and indent levels
authored
188 if(!r)
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
189 return CURLE_OUT_OF_MEMORY;
190
191 RTMP_Init(r);
192 RTMP_SetBufferMS(r, DEF_BUFTIME);
b903186 @bagder source cleanup: unify look, style and indent levels
authored
193 if(!RTMP_SetupURL(r, conn->data->change.url)) {
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
194 RTMP_Free(r);
195 return CURLE_URL_MALFORMAT;
196 }
197 conn->proto.generic = r;
198 return CURLE_OK;
199 }
200
201 static CURLcode rtmp_connect(struct connectdata *conn, bool *done)
202 {
203 RTMP *r = conn->proto.generic;
204 SET_RCVTIMEO(tv,10);
205
206 r->m_sb.sb_socket = conn->sock[FIRSTSOCKET];
207
208 /* We have to know if it's a write before we send the
209 * connect request packet
210 */
b903186 @bagder source cleanup: unify look, style and indent levels
authored
211 if(conn->data->set.upload)
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
212 r->Link.protocol |= RTMP_FEATURE_WRITE;
213
214 /* For plain streams, use the buffer toggle trick to keep data flowing */
b903186 @bagder source cleanup: unify look, style and indent levels
authored
215 if(!(r->Link.lFlags & RTMP_LF_LIVE) &&
216 !(r->Link.protocol & RTMP_FEATURE_HTTP))
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
217 r->Link.lFlags |= RTMP_LF_BUFX;
218
219 curlx_nonblock(r->m_sb.sb_socket, FALSE);
b903186 @bagder source cleanup: unify look, style and indent levels
authored
220 setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO,
221 (char *)&tv, sizeof(tv));
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
222
b903186 @bagder source cleanup: unify look, style and indent levels
authored
223 if(!RTMP_Connect1(r, NULL))
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
224 return CURLE_FAILED_INIT;
225
226 /* Clients must send a periodic BytesReceived report to the server */
227 r->m_bSendCounter = true;
228
229 *done = TRUE;
230 conn->recv[FIRSTSOCKET] = rtmp_recv;
231 conn->send[FIRSTSOCKET] = rtmp_send;
232 return CURLE_OK;
233 }
234
235 static CURLcode rtmp_do(struct connectdata *conn, bool *done)
236 {
237 RTMP *r = conn->proto.generic;
238
b903186 @bagder source cleanup: unify look, style and indent levels
authored
239 if(!RTMP_ConnectStream(r, 0))
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
240 return CURLE_FAILED_INIT;
241
b903186 @bagder source cleanup: unify look, style and indent levels
authored
242 if(conn->data->set.upload) {
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
243 Curl_pgrsSetUploadSize(conn->data, conn->data->set.infilesize);
244 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, FIRSTSOCKET, NULL);
b903186 @bagder source cleanup: unify look, style and indent levels
authored
245 }
246 else
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
247 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
248 *done = TRUE;
249 return CURLE_OK;
250 }
251
252 static CURLcode rtmp_done(struct connectdata *conn, CURLcode status,
253 bool premature)
254 {
d3c813e @jchaffraix RTMP: Fix compiler warnings
jchaffraix authored
255 (void)conn; /* unused */
256 (void)status; /* unused */
257 (void)premature; /* unused */
258
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
259 return CURLE_OK;
260 }
261
5965d45 @bagder disconnect: pass on the dead_connection argument
authored
262 static CURLcode rtmp_disconnect(struct connectdata *conn,
263 bool dead_connection)
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
264 {
265 RTMP *r = conn->proto.generic;
5965d45 @bagder disconnect: pass on the dead_connection argument
authored
266 (void)dead_connection;
b903186 @bagder source cleanup: unify look, style and indent levels
authored
267 if(r) {
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
268 conn->proto.generic = NULL;
269 RTMP_Close(r);
270 RTMP_Free(r);
271 }
272 return CURLE_OK;
273 }
274
275 static ssize_t rtmp_recv(struct connectdata *conn, int sockindex, char *buf,
276 size_t len, CURLcode *err)
277 {
278 RTMP *r = conn->proto.generic;
279 ssize_t nread;
280
d3c813e @jchaffraix RTMP: Fix compiler warnings
jchaffraix authored
281 (void)sockindex; /* unused */
282
dee2ef8 @mback2k krb5/curl_rtmp.c: Hide size_t to int type conversion warning
mback2k authored
283 nread = RTMP_Read(r, buf, curlx_uztosi(len));
b903186 @bagder source cleanup: unify look, style and indent levels
authored
284 if(nread < 0) {
285 if(r->m_read.status == RTMP_READ_COMPLETE ||
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
286 r->m_read.status == RTMP_READ_EOF) {
287 conn->data->req.size = conn->data->req.bytecount;
288 nread = 0;
b903186 @bagder source cleanup: unify look, style and indent levels
authored
289 }
290 else
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
291 *err = CURLE_RECV_ERROR;
292 }
293 return nread;
294 }
295
296 static ssize_t rtmp_send(struct connectdata *conn, int sockindex,
297 const void *buf, size_t len, CURLcode *err)
298 {
299 RTMP *r = conn->proto.generic;
300 ssize_t num;
301
d3c813e @jchaffraix RTMP: Fix compiler warnings
jchaffraix authored
302 (void)sockindex; /* unused */
303
dee2ef8 @mback2k krb5/curl_rtmp.c: Hide size_t to int type conversion warning
mback2k authored
304 num = RTMP_Write(r, (char *)buf, curlx_uztosi(len));
b903186 @bagder source cleanup: unify look, style and indent levels
authored
305 if(num < 0)
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
306 *err = CURLE_SEND_ERROR;
b903186 @bagder source cleanup: unify look, style and indent levels
authored
307
04cb15a RTMP: initial support added, powered by librtmp
Howard Chu authored
308 return num;
309 }
310 #endif /* USE_LIBRTMP */
Something went wrong with that request. Please try again.