curl_rtmp.c 11 KB

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