curl_rtmp.c 11 KB

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