curl_rtmp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2012 - 2015, 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 https://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 "curl_rtmp.h"
  26. #include "urldata.h"
  27. #include "nonblock.h" /* for curlx_nonblock */
  28. #include "progress.h" /* for Curl_pgrsSetUploadSize */
  29. #include "transfer.h"
  30. #include "warnless.h"
  31. #include <curl/curl.h>
  32. #include <librtmp/rtmp.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_connection(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 https://rtmpdump.mplayerhq.hu
  52. */
  53. const struct Curl_handler Curl_handler_rtmp = {
  54. "RTMP", /* scheme */
  55. rtmp_setup_connection, /* 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. ZERO_NULL, /* connection_check */
  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_connection, /* 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. ZERO_NULL, /* connection_check */
  89. PORT_RTMPT, /* defport */
  90. CURLPROTO_RTMPT, /* protocol */
  91. PROTOPT_NONE /* flags*/
  92. };
  93. const struct Curl_handler Curl_handler_rtmpe = {
  94. "RTMPE", /* scheme */
  95. rtmp_setup_connection, /* setup_connection */
  96. rtmp_do, /* do_it */
  97. rtmp_done, /* done */
  98. ZERO_NULL, /* do_more */
  99. rtmp_connect, /* connect_it */
  100. ZERO_NULL, /* connecting */
  101. ZERO_NULL, /* doing */
  102. ZERO_NULL, /* proto_getsock */
  103. ZERO_NULL, /* doing_getsock */
  104. ZERO_NULL, /* domore_getsock */
  105. ZERO_NULL, /* perform_getsock */
  106. rtmp_disconnect, /* disconnect */
  107. ZERO_NULL, /* readwrite */
  108. ZERO_NULL, /* connection_check */
  109. PORT_RTMP, /* defport */
  110. CURLPROTO_RTMPE, /* protocol */
  111. PROTOPT_NONE /* flags*/
  112. };
  113. const struct Curl_handler Curl_handler_rtmpte = {
  114. "RTMPTE", /* scheme */
  115. rtmp_setup_connection, /* setup_connection */
  116. rtmp_do, /* do_it */
  117. rtmp_done, /* done */
  118. ZERO_NULL, /* do_more */
  119. rtmp_connect, /* connect_it */
  120. ZERO_NULL, /* connecting */
  121. ZERO_NULL, /* doing */
  122. ZERO_NULL, /* proto_getsock */
  123. ZERO_NULL, /* doing_getsock */
  124. ZERO_NULL, /* domore_getsock */
  125. ZERO_NULL, /* perform_getsock */
  126. rtmp_disconnect, /* disconnect */
  127. ZERO_NULL, /* readwrite */
  128. ZERO_NULL, /* connection_check */
  129. PORT_RTMPT, /* defport */
  130. CURLPROTO_RTMPTE, /* protocol */
  131. PROTOPT_NONE /* flags*/
  132. };
  133. const struct Curl_handler Curl_handler_rtmps = {
  134. "RTMPS", /* scheme */
  135. rtmp_setup_connection, /* setup_connection */
  136. rtmp_do, /* do_it */
  137. rtmp_done, /* done */
  138. ZERO_NULL, /* do_more */
  139. rtmp_connect, /* connect_it */
  140. ZERO_NULL, /* connecting */
  141. ZERO_NULL, /* doing */
  142. ZERO_NULL, /* proto_getsock */
  143. ZERO_NULL, /* doing_getsock */
  144. ZERO_NULL, /* domore_getsock */
  145. ZERO_NULL, /* perform_getsock */
  146. rtmp_disconnect, /* disconnect */
  147. ZERO_NULL, /* readwrite */
  148. ZERO_NULL, /* connection_check */
  149. PORT_RTMPS, /* defport */
  150. CURLPROTO_RTMPS, /* protocol */
  151. PROTOPT_NONE /* flags*/
  152. };
  153. const struct Curl_handler Curl_handler_rtmpts = {
  154. "RTMPTS", /* scheme */
  155. rtmp_setup_connection, /* setup_connection */
  156. rtmp_do, /* do_it */
  157. rtmp_done, /* done */
  158. ZERO_NULL, /* do_more */
  159. rtmp_connect, /* connect_it */
  160. ZERO_NULL, /* connecting */
  161. ZERO_NULL, /* doing */
  162. ZERO_NULL, /* proto_getsock */
  163. ZERO_NULL, /* doing_getsock */
  164. ZERO_NULL, /* domore_getsock */
  165. ZERO_NULL, /* perform_getsock */
  166. rtmp_disconnect, /* disconnect */
  167. ZERO_NULL, /* readwrite */
  168. ZERO_NULL, /* connection_check */
  169. PORT_RTMPS, /* defport */
  170. CURLPROTO_RTMPTS, /* protocol */
  171. PROTOPT_NONE /* flags*/
  172. };
  173. static CURLcode rtmp_setup_connection(struct connectdata *conn)
  174. {
  175. RTMP *r = RTMP_Alloc();
  176. if(!r)
  177. return CURLE_OUT_OF_MEMORY;
  178. RTMP_Init(r);
  179. RTMP_SetBufferMS(r, DEF_BUFTIME);
  180. if(!RTMP_SetupURL(r, conn->data->change.url)) {
  181. RTMP_Free(r);
  182. return CURLE_URL_MALFORMAT;
  183. }
  184. conn->proto.generic = r;
  185. return CURLE_OK;
  186. }
  187. static CURLcode rtmp_connect(struct connectdata *conn, bool *done)
  188. {
  189. RTMP *r = conn->proto.generic;
  190. SET_RCVTIMEO(tv, 10);
  191. r->m_sb.sb_socket = (int)conn->sock[FIRSTSOCKET];
  192. /* We have to know if it's a write before we send the
  193. * connect request packet
  194. */
  195. if(conn->data->set.upload)
  196. r->Link.protocol |= RTMP_FEATURE_WRITE;
  197. /* For plain streams, use the buffer toggle trick to keep data flowing */
  198. if(!(r->Link.lFlags & RTMP_LF_LIVE) &&
  199. !(r->Link.protocol & RTMP_FEATURE_HTTP))
  200. r->Link.lFlags |= RTMP_LF_BUFX;
  201. (void)curlx_nonblock(r->m_sb.sb_socket, FALSE);
  202. setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO,
  203. (char *)&tv, sizeof(tv));
  204. if(!RTMP_Connect1(r, NULL))
  205. return CURLE_FAILED_INIT;
  206. /* Clients must send a periodic BytesReceived report to the server */
  207. r->m_bSendCounter = true;
  208. *done = TRUE;
  209. conn->recv[FIRSTSOCKET] = rtmp_recv;
  210. conn->send[FIRSTSOCKET] = rtmp_send;
  211. return CURLE_OK;
  212. }
  213. static CURLcode rtmp_do(struct connectdata *conn, bool *done)
  214. {
  215. RTMP *r = conn->proto.generic;
  216. if(!RTMP_ConnectStream(r, 0))
  217. return CURLE_FAILED_INIT;
  218. if(conn->data->set.upload) {
  219. Curl_pgrsSetUploadSize(conn->data, conn->data->state.infilesize);
  220. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, FIRSTSOCKET, NULL);
  221. }
  222. else
  223. Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
  224. *done = TRUE;
  225. return CURLE_OK;
  226. }
  227. static CURLcode rtmp_done(struct connectdata *conn, CURLcode status,
  228. bool premature)
  229. {
  230. (void)conn; /* unused */
  231. (void)status; /* unused */
  232. (void)premature; /* unused */
  233. return CURLE_OK;
  234. }
  235. static CURLcode rtmp_disconnect(struct connectdata *conn,
  236. bool dead_connection)
  237. {
  238. RTMP *r = conn->proto.generic;
  239. (void)dead_connection;
  240. if(r) {
  241. conn->proto.generic = NULL;
  242. RTMP_Close(r);
  243. RTMP_Free(r);
  244. }
  245. return CURLE_OK;
  246. }
  247. static ssize_t rtmp_recv(struct connectdata *conn, int sockindex, char *buf,
  248. size_t len, CURLcode *err)
  249. {
  250. RTMP *r = conn->proto.generic;
  251. ssize_t nread;
  252. (void)sockindex; /* unused */
  253. nread = RTMP_Read(r, buf, curlx_uztosi(len));
  254. if(nread < 0) {
  255. if(r->m_read.status == RTMP_READ_COMPLETE ||
  256. r->m_read.status == RTMP_READ_EOF) {
  257. conn->data->req.size = conn->data->req.bytecount;
  258. nread = 0;
  259. }
  260. else
  261. *err = CURLE_RECV_ERROR;
  262. }
  263. return nread;
  264. }
  265. static ssize_t rtmp_send(struct connectdata *conn, int sockindex,
  266. const void *buf, size_t len, CURLcode *err)
  267. {
  268. RTMP *r = conn->proto.generic;
  269. ssize_t num;
  270. (void)sockindex; /* unused */
  271. num = RTMP_Write(r, (char *)buf, curlx_uztosi(len));
  272. if(num < 0)
  273. *err = CURLE_SEND_ERROR;
  274. return num;
  275. }
  276. #endif /* USE_LIBRTMP */