curl_rtmp.c 12 KB

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