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_connection);
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 PORT_RTMP, /* defport */
77 PROT_RTMP /* protocol */
80 const struct Curl_handler Curl_handler_rtmpt = {
82 rtmp_setup, /* setup_connection */
85 ZERO_NULL, /* do_more */
86 rtmp_connect, /* connect_it */
87 ZERO_NULL, /* connecting */
88 ZERO_NULL, /* doing */
89 ZERO_NULL, /* proto_getsock */
90 ZERO_NULL, /* doing_getsock */
91 ZERO_NULL, /* perform_getsock */
92 rtmp_disconnect, /* disconnect */
93 PORT_RTMPT, /* defport */
94 PROT_RTMPT /* protocol */
97 const struct Curl_handler Curl_handler_rtmpe = {
99 rtmp_setup, /* setup_connection */
101 rtmp_done, /* done */
102 ZERO_NULL, /* do_more */
103 rtmp_connect, /* connect_it */
104 ZERO_NULL, /* connecting */
105 ZERO_NULL, /* doing */
106 ZERO_NULL, /* proto_getsock */
107 ZERO_NULL, /* doing_getsock */
108 ZERO_NULL, /* perform_getsock */
109 rtmp_disconnect, /* disconnect */
110 PORT_RTMP, /* defport */
111 PROT_RTMPE /* protocol */
114 const struct Curl_handler Curl_handler_rtmpte = {
115 "RTMPTE", /* scheme */
116 rtmp_setup, /* setup_connection */
118 rtmp_done, /* done */
119 ZERO_NULL, /* do_more */
120 rtmp_connect, /* connect_it */
121 ZERO_NULL, /* connecting */
122 ZERO_NULL, /* doing */
123 ZERO_NULL, /* proto_getsock */
124 ZERO_NULL, /* doing_getsock */
125 ZERO_NULL, /* perform_getsock */
126 rtmp_disconnect, /* disconnect */
127 PORT_RTMPT, /* defport */
128 PROT_RTMPTE /* protocol */
131 const struct Curl_handler Curl_handler_rtmps = {
132 "RTMPS", /* scheme */
133 rtmp_setup, /* setup_connection */
135 rtmp_done, /* done */
136 ZERO_NULL, /* do_more */
137 rtmp_connect, /* connect_it */
138 ZERO_NULL, /* connecting */
139 ZERO_NULL, /* doing */
140 ZERO_NULL, /* proto_getsock */
141 ZERO_NULL, /* doing_getsock */
142 ZERO_NULL, /* perform_getsock */
143 rtmp_disconnect, /* disconnect */
144 PORT_RTMPS, /* defport */
145 PROT_RTMPS /* protocol */
147 const struct Curl_handler Curl_handler_rtmpts = {
148 "RTMPTS", /* scheme */
149 rtmp_setup, /* setup_connection */
151 rtmp_done, /* done */
152 ZERO_NULL, /* do_more */
153 rtmp_connect, /* connect_it */
154 ZERO_NULL, /* connecting */
155 ZERO_NULL, /* doing */
156 ZERO_NULL, /* proto_getsock */
157 ZERO_NULL, /* doing_getsock */
158 ZERO_NULL, /* perform_getsock */
159 rtmp_disconnect, /* disconnect */
160 PORT_RTMPS, /* defport */
161 PROT_RTMPTS /* protocol */
164 static CURLcode rtmp_setup(struct connectdata *conn)
166 RTMP *r = RTMP_Alloc();
169 return CURLE_OUT_OF_MEMORY;
172 RTMP_SetBufferMS(r, DEF_BUFTIME);
173 if (!RTMP_SetupURL(r, conn->data->change.url)) {
175 return CURLE_URL_MALFORMAT;
177 conn->proto.generic = r;
181 static CURLcode rtmp_connect(struct connectdata *conn, bool *done)
183 RTMP *r = conn->proto.generic;
186 r->m_sb.sb_socket = conn->sock[FIRSTSOCKET];
188 /* We have to know if it's a write before we send the
189 * connect request packet
191 if (conn->data->set.upload)
192 r->Link.protocol |= RTMP_FEATURE_WRITE;
194 /* For plain streams, use the buffer toggle trick to keep data flowing */
195 if (!(r->Link.lFlags & RTMP_LF_LIVE) && !(r->Link.protocol & RTMP_FEATURE_HTTP))
196 r->Link.lFlags |= RTMP_LF_BUFX;
198 curlx_nonblock(r->m_sb.sb_socket, FALSE);
199 setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
201 if (!RTMP_Connect1(r, NULL))
202 return CURLE_FAILED_INIT;
204 /* Clients must send a periodic BytesReceived report to the server */
205 r->m_bSendCounter = true;
208 conn->recv[FIRSTSOCKET] = rtmp_recv;
209 conn->send[FIRSTSOCKET] = rtmp_send;
213 static CURLcode rtmp_do(struct connectdata *conn, bool *done)
215 RTMP *r = conn->proto.generic;
217 if (!RTMP_ConnectStream(r, 0))
218 return CURLE_FAILED_INIT;
220 if (conn->data->set.upload) {
221 Curl_pgrsSetUploadSize(conn->data, conn->data->set.infilesize);
222 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, FIRSTSOCKET, NULL);
224 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
229 static CURLcode rtmp_done(struct connectdata *conn, CURLcode status,
232 (void)conn; /* unused */
233 (void)status; /* unused */
234 (void)premature; /* unused */
239 static CURLcode rtmp_disconnect(struct connectdata *conn,
240 bool dead_connection)
242 RTMP *r = conn->proto.generic;
243 (void)dead_connection;
245 conn->proto.generic = NULL;
252 static ssize_t rtmp_recv(struct connectdata *conn, int sockindex, char *buf,
253 size_t len, CURLcode *err)
255 RTMP *r = conn->proto.generic;
258 (void)sockindex; /* unused */
260 nread = RTMP_Read(r, buf, len);
262 if (r->m_read.status == RTMP_READ_COMPLETE ||
263 r->m_read.status == RTMP_READ_EOF) {
264 conn->data->req.size = conn->data->req.bytecount;
267 *err = CURLE_RECV_ERROR;
272 static ssize_t rtmp_send(struct connectdata *conn, int sockindex,
273 const void *buf, size_t len, CURLcode *err)
275 RTMP *r = conn->proto.generic;
278 (void)sockindex; /* unused */
280 num = RTMP_Write(r, (char *)buf, len);
282 *err = CURLE_SEND_ERROR;
286 #endif /* USE_LIBRTMP */