1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2010, Howard Chu, <hyc@highlandsun.com>
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
28 #include "nonblock.h" /* for curlx_nonblock */
29 #include "progress.h" /* for Curl_pgrsSetUploadSize */
31 #include <curl/curl.h>
32 #include <librtmp/rtmp.h>
34 #define _MPRINTF_REPLACE /* use our functions only */
35 #include <curl/mprintf.h>
37 #include "curl_memory.h"
38 /* The last #include file should be: */
42 #define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
43 #define SET_RCVTIMEO(tv,s) int tv = s*1000
45 #define SET_RCVTIMEO(tv,s) struct timeval tv = {s,0}
48 #define DEF_BUFTIME (2*60*60*1000) /* 2 hours */
50 static CURLcode rtmp_setup(struct connectdata *conn);
51 static CURLcode rtmp_do(struct connectdata *conn, bool *done);
52 static CURLcode rtmp_done(struct connectdata *conn, CURLcode, bool premature);
53 static CURLcode rtmp_connect(struct connectdata *conn, bool *done);
54 static CURLcode rtmp_disconnect(struct connectdata *conn, bool dead);
56 static Curl_recv rtmp_recv;
57 static Curl_send rtmp_send;
60 * RTMP protocol handler.h, based on http://rtmpdump.mplayerhq.hu
63 const struct Curl_handler Curl_handler_rtmp = {
65 rtmp_setup, /* setup_connection */
68 ZERO_NULL, /* do_more */
69 rtmp_connect, /* connect_it */
70 ZERO_NULL, /* connecting */
71 ZERO_NULL, /* doing */
72 ZERO_NULL, /* proto_getsock */
73 ZERO_NULL, /* doing_getsock */
74 ZERO_NULL, /* perform_getsock */
75 rtmp_disconnect, /* disconnect */
76 ZERO_NULL, /* readwrite */
77 PORT_RTMP, /* defport */
78 CURLPROTO_RTMP, /* protocol */
79 PROTOPT_NONE /* flags*/
82 const struct Curl_handler Curl_handler_rtmpt = {
84 rtmp_setup, /* setup_connection */
87 ZERO_NULL, /* do_more */
88 rtmp_connect, /* connect_it */
89 ZERO_NULL, /* connecting */
90 ZERO_NULL, /* doing */
91 ZERO_NULL, /* proto_getsock */
92 ZERO_NULL, /* doing_getsock */
93 ZERO_NULL, /* perform_getsock */
94 rtmp_disconnect, /* disconnect */
95 ZERO_NULL, /* readwrite */
96 PORT_RTMPT, /* defport */
97 CURLPROTO_RTMPT, /* protocol */
98 PROTOPT_NONE /* flags*/
101 const struct Curl_handler Curl_handler_rtmpe = {
102 "RTMPE", /* scheme */
103 rtmp_setup, /* setup_connection */
105 rtmp_done, /* done */
106 ZERO_NULL, /* do_more */
107 rtmp_connect, /* connect_it */
108 ZERO_NULL, /* connecting */
109 ZERO_NULL, /* doing */
110 ZERO_NULL, /* proto_getsock */
111 ZERO_NULL, /* doing_getsock */
112 ZERO_NULL, /* perform_getsock */
113 rtmp_disconnect, /* disconnect */
114 ZERO_NULL, /* readwrite */
115 PORT_RTMP, /* defport */
116 CURLPROTO_RTMPE, /* protocol */
117 PROTOPT_NONE /* flags*/
120 const struct Curl_handler Curl_handler_rtmpte = {
121 "RTMPTE", /* scheme */
122 rtmp_setup, /* setup_connection */
124 rtmp_done, /* done */
125 ZERO_NULL, /* do_more */
126 rtmp_connect, /* connect_it */
127 ZERO_NULL, /* connecting */
128 ZERO_NULL, /* doing */
129 ZERO_NULL, /* proto_getsock */
130 ZERO_NULL, /* doing_getsock */
131 ZERO_NULL, /* perform_getsock */
132 rtmp_disconnect, /* disconnect */
133 ZERO_NULL, /* readwrite */
134 PORT_RTMPT, /* defport */
135 CURLPROTO_RTMPTE, /* protocol */
136 PROTOPT_NONE /* flags*/
139 const struct Curl_handler Curl_handler_rtmps = {
140 "RTMPS", /* scheme */
141 rtmp_setup, /* setup_connection */
143 rtmp_done, /* done */
144 ZERO_NULL, /* do_more */
145 rtmp_connect, /* connect_it */
146 ZERO_NULL, /* connecting */
147 ZERO_NULL, /* doing */
148 ZERO_NULL, /* proto_getsock */
149 ZERO_NULL, /* doing_getsock */
150 ZERO_NULL, /* perform_getsock */
151 rtmp_disconnect, /* disconnect */
152 ZERO_NULL, /* readwrite */
153 PORT_RTMPS, /* defport */
154 CURLPROTO_RTMPS, /* protocol */
155 PROTOPT_NONE /* flags*/
158 const struct Curl_handler Curl_handler_rtmpts = {
159 "RTMPTS", /* scheme */
160 rtmp_setup, /* setup_connection */
162 rtmp_done, /* done */
163 ZERO_NULL, /* do_more */
164 rtmp_connect, /* connect_it */
165 ZERO_NULL, /* connecting */
166 ZERO_NULL, /* doing */
167 ZERO_NULL, /* proto_getsock */
168 ZERO_NULL, /* doing_getsock */
169 ZERO_NULL, /* perform_getsock */
170 rtmp_disconnect, /* disconnect */
171 ZERO_NULL, /* readwrite */
172 PORT_RTMPS, /* defport */
173 CURLPROTO_RTMPTS, /* protocol */
174 PROTOPT_NONE /* flags*/
177 static CURLcode rtmp_setup(struct connectdata *conn)
179 RTMP *r = RTMP_Alloc();
182 return CURLE_OUT_OF_MEMORY;
185 RTMP_SetBufferMS(r, DEF_BUFTIME);
186 if(!RTMP_SetupURL(r, conn->data->change.url)) {
188 return CURLE_URL_MALFORMAT;
190 conn->proto.generic = r;
194 static CURLcode rtmp_connect(struct connectdata *conn, bool *done)
196 RTMP *r = conn->proto.generic;
199 r->m_sb.sb_socket = conn->sock[FIRSTSOCKET];
201 /* We have to know if it's a write before we send the
202 * connect request packet
204 if(conn->data->set.upload)
205 r->Link.protocol |= RTMP_FEATURE_WRITE;
207 /* For plain streams, use the buffer toggle trick to keep data flowing */
208 if(!(r->Link.lFlags & RTMP_LF_LIVE) &&
209 !(r->Link.protocol & RTMP_FEATURE_HTTP))
210 r->Link.lFlags |= RTMP_LF_BUFX;
212 curlx_nonblock(r->m_sb.sb_socket, FALSE);
213 setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO,
214 (char *)&tv, sizeof(tv));
216 if(!RTMP_Connect1(r, NULL))
217 return CURLE_FAILED_INIT;
219 /* Clients must send a periodic BytesReceived report to the server */
220 r->m_bSendCounter = true;
223 conn->recv[FIRSTSOCKET] = rtmp_recv;
224 conn->send[FIRSTSOCKET] = rtmp_send;
228 static CURLcode rtmp_do(struct connectdata *conn, bool *done)
230 RTMP *r = conn->proto.generic;
232 if(!RTMP_ConnectStream(r, 0))
233 return CURLE_FAILED_INIT;
235 if(conn->data->set.upload) {
236 Curl_pgrsSetUploadSize(conn->data, conn->data->set.infilesize);
237 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, FIRSTSOCKET, NULL);
240 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
245 static CURLcode rtmp_done(struct connectdata *conn, CURLcode status,
248 (void)conn; /* unused */
249 (void)status; /* unused */
250 (void)premature; /* unused */
255 static CURLcode rtmp_disconnect(struct connectdata *conn,
256 bool dead_connection)
258 RTMP *r = conn->proto.generic;
259 (void)dead_connection;
261 conn->proto.generic = NULL;
268 static ssize_t rtmp_recv(struct connectdata *conn, int sockindex, char *buf,
269 size_t len, CURLcode *err)
271 RTMP *r = conn->proto.generic;
274 (void)sockindex; /* unused */
276 nread = RTMP_Read(r, buf, len);
278 if(r->m_read.status == RTMP_READ_COMPLETE ||
279 r->m_read.status == RTMP_READ_EOF) {
280 conn->data->req.size = conn->data->req.bytecount;
284 *err = CURLE_RECV_ERROR;
289 static ssize_t rtmp_send(struct connectdata *conn, int sockindex,
290 const void *buf, size_t len, CURLcode *err)
292 RTMP *r = conn->proto.generic;
295 (void)sockindex; /* unused */
297 num = RTMP_Write(r, (char *)buf, len);
299 *err = CURLE_SEND_ERROR;
303 #endif /* USE_LIBRTMP */