curl_rtmp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  23. *
  24. ***************************************************************************/
  25. #include "curl_setup.h"
  26. #ifdef USE_LIBRTMP
  27. #include "curl_rtmp.h"
  28. #include "urldata.h"
  29. #include "nonblock.h" /* for curlx_nonblock */
  30. #include "progress.h" /* for Curl_pgrsSetUploadSize */
  31. #include "transfer.h"
  32. #include "warnless.h"
  33. #include <curl/curl.h>
  34. #include <librtmp/rtmp.h>
  35. /* The last 3 #include files should be in this order */
  36. #include "curl_printf.h"
  37. #include "curl_memory.h"
  38. #include "memdebug.h"
  39. #if defined(_WIN32) && !defined(USE_LWIPSOCK)
  40. #define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
  41. #define SET_RCVTIMEO(tv,s) int tv = s*1000
  42. #elif defined(LWIP_SO_SNDRCVTIMEO_NONSTANDARD)
  43. #define SET_RCVTIMEO(tv,s) int tv = s*1000
  44. #else
  45. #define SET_RCVTIMEO(tv,s) struct timeval tv = {s,0}
  46. #endif
  47. #define DEF_BUFTIME (2*60*60*1000) /* 2 hours */
  48. static CURLcode rtmp_setup_connection(struct Curl_easy *data,
  49. struct connectdata *conn);
  50. static CURLcode rtmp_do(struct Curl_easy *data, bool *done);
  51. static CURLcode rtmp_done(struct Curl_easy *data, CURLcode, bool premature);
  52. static CURLcode rtmp_connect(struct Curl_easy *data, bool *done);
  53. static CURLcode rtmp_disconnect(struct Curl_easy *data,
  54. struct connectdata *conn, bool dead);
  55. static Curl_recv rtmp_recv;
  56. static Curl_send rtmp_send;
  57. /*
  58. * RTMP protocol handler.h, based on https://rtmpdump.mplayerhq.hu
  59. */
  60. const struct Curl_handler Curl_handler_rtmp = {
  61. "rtmp", /* scheme */
  62. rtmp_setup_connection, /* setup_connection */
  63. rtmp_do, /* do_it */
  64. rtmp_done, /* done */
  65. ZERO_NULL, /* do_more */
  66. rtmp_connect, /* connect_it */
  67. ZERO_NULL, /* connecting */
  68. ZERO_NULL, /* doing */
  69. ZERO_NULL, /* proto_getsock */
  70. ZERO_NULL, /* doing_getsock */
  71. ZERO_NULL, /* domore_getsock */
  72. ZERO_NULL, /* perform_getsock */
  73. rtmp_disconnect, /* disconnect */
  74. ZERO_NULL, /* write_resp */
  75. ZERO_NULL, /* write_resp_hd */
  76. ZERO_NULL, /* connection_check */
  77. ZERO_NULL, /* attach connection */
  78. PORT_RTMP, /* defport */
  79. CURLPROTO_RTMP, /* protocol */
  80. CURLPROTO_RTMP, /* family */
  81. PROTOPT_NONE /* flags */
  82. };
  83. const struct Curl_handler Curl_handler_rtmpt = {
  84. "rtmpt", /* scheme */
  85. rtmp_setup_connection, /* setup_connection */
  86. rtmp_do, /* do_it */
  87. rtmp_done, /* done */
  88. ZERO_NULL, /* do_more */
  89. rtmp_connect, /* connect_it */
  90. ZERO_NULL, /* connecting */
  91. ZERO_NULL, /* doing */
  92. ZERO_NULL, /* proto_getsock */
  93. ZERO_NULL, /* doing_getsock */
  94. ZERO_NULL, /* domore_getsock */
  95. ZERO_NULL, /* perform_getsock */
  96. rtmp_disconnect, /* disconnect */
  97. ZERO_NULL, /* write_resp */
  98. ZERO_NULL, /* write_resp_hd */
  99. ZERO_NULL, /* connection_check */
  100. ZERO_NULL, /* attach connection */
  101. PORT_RTMPT, /* defport */
  102. CURLPROTO_RTMPT, /* protocol */
  103. CURLPROTO_RTMPT, /* family */
  104. PROTOPT_NONE /* flags */
  105. };
  106. const struct Curl_handler Curl_handler_rtmpe = {
  107. "rtmpe", /* scheme */
  108. rtmp_setup_connection, /* setup_connection */
  109. rtmp_do, /* do_it */
  110. rtmp_done, /* done */
  111. ZERO_NULL, /* do_more */
  112. rtmp_connect, /* connect_it */
  113. ZERO_NULL, /* connecting */
  114. ZERO_NULL, /* doing */
  115. ZERO_NULL, /* proto_getsock */
  116. ZERO_NULL, /* doing_getsock */
  117. ZERO_NULL, /* domore_getsock */
  118. ZERO_NULL, /* perform_getsock */
  119. rtmp_disconnect, /* disconnect */
  120. ZERO_NULL, /* write_resp */
  121. ZERO_NULL, /* write_resp_hd */
  122. ZERO_NULL, /* connection_check */
  123. ZERO_NULL, /* attach connection */
  124. PORT_RTMP, /* defport */
  125. CURLPROTO_RTMPE, /* protocol */
  126. CURLPROTO_RTMPE, /* family */
  127. PROTOPT_NONE /* flags */
  128. };
  129. const struct Curl_handler Curl_handler_rtmpte = {
  130. "rtmpte", /* scheme */
  131. rtmp_setup_connection, /* 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, /* write_resp */
  144. ZERO_NULL, /* write_resp_hd */
  145. ZERO_NULL, /* connection_check */
  146. ZERO_NULL, /* attach connection */
  147. PORT_RTMPT, /* defport */
  148. CURLPROTO_RTMPTE, /* protocol */
  149. CURLPROTO_RTMPTE, /* family */
  150. PROTOPT_NONE /* flags */
  151. };
  152. const struct Curl_handler Curl_handler_rtmps = {
  153. "rtmps", /* scheme */
  154. rtmp_setup_connection, /* setup_connection */
  155. rtmp_do, /* do_it */
  156. rtmp_done, /* done */
  157. ZERO_NULL, /* do_more */
  158. rtmp_connect, /* connect_it */
  159. ZERO_NULL, /* connecting */
  160. ZERO_NULL, /* doing */
  161. ZERO_NULL, /* proto_getsock */
  162. ZERO_NULL, /* doing_getsock */
  163. ZERO_NULL, /* domore_getsock */
  164. ZERO_NULL, /* perform_getsock */
  165. rtmp_disconnect, /* disconnect */
  166. ZERO_NULL, /* write_resp */
  167. ZERO_NULL, /* write_resp_hd */
  168. ZERO_NULL, /* connection_check */
  169. ZERO_NULL, /* attach connection */
  170. PORT_RTMPS, /* defport */
  171. CURLPROTO_RTMPS, /* protocol */
  172. CURLPROTO_RTMP, /* family */
  173. PROTOPT_NONE /* flags */
  174. };
  175. const struct Curl_handler Curl_handler_rtmpts = {
  176. "rtmpts", /* scheme */
  177. rtmp_setup_connection, /* setup_connection */
  178. rtmp_do, /* do_it */
  179. rtmp_done, /* done */
  180. ZERO_NULL, /* do_more */
  181. rtmp_connect, /* connect_it */
  182. ZERO_NULL, /* connecting */
  183. ZERO_NULL, /* doing */
  184. ZERO_NULL, /* proto_getsock */
  185. ZERO_NULL, /* doing_getsock */
  186. ZERO_NULL, /* domore_getsock */
  187. ZERO_NULL, /* perform_getsock */
  188. rtmp_disconnect, /* disconnect */
  189. ZERO_NULL, /* write_resp */
  190. ZERO_NULL, /* write_resp_hd */
  191. ZERO_NULL, /* connection_check */
  192. ZERO_NULL, /* attach connection */
  193. PORT_RTMPS, /* defport */
  194. CURLPROTO_RTMPTS, /* protocol */
  195. CURLPROTO_RTMPT, /* family */
  196. PROTOPT_NONE /* flags */
  197. };
  198. static CURLcode rtmp_setup_connection(struct Curl_easy *data,
  199. struct connectdata *conn)
  200. {
  201. RTMP *r = RTMP_Alloc();
  202. if(!r)
  203. return CURLE_OUT_OF_MEMORY;
  204. RTMP_Init(r);
  205. RTMP_SetBufferMS(r, DEF_BUFTIME);
  206. if(!RTMP_SetupURL(r, data->state.url)) {
  207. RTMP_Free(r);
  208. return CURLE_URL_MALFORMAT;
  209. }
  210. conn->proto.rtmp = r;
  211. return CURLE_OK;
  212. }
  213. static CURLcode rtmp_connect(struct Curl_easy *data, bool *done)
  214. {
  215. struct connectdata *conn = data->conn;
  216. RTMP *r = conn->proto.rtmp;
  217. SET_RCVTIMEO(tv, 10);
  218. r->m_sb.sb_socket = (int)conn->sock[FIRSTSOCKET];
  219. /* We have to know if it is a write before we send the
  220. * connect request packet
  221. */
  222. if(data->state.upload)
  223. r->Link.protocol |= RTMP_FEATURE_WRITE;
  224. /* For plain streams, use the buffer toggle trick to keep data flowing */
  225. if(!(r->Link.lFlags & RTMP_LF_LIVE) &&
  226. !(r->Link.protocol & RTMP_FEATURE_HTTP))
  227. r->Link.lFlags |= RTMP_LF_BUFX;
  228. (void)curlx_nonblock(r->m_sb.sb_socket, FALSE);
  229. setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO,
  230. (char *)&tv, sizeof(tv));
  231. if(!RTMP_Connect1(r, NULL))
  232. return CURLE_FAILED_INIT;
  233. /* Clients must send a periodic BytesReceived report to the server */
  234. r->m_bSendCounter = true;
  235. *done = TRUE;
  236. conn->recv[FIRSTSOCKET] = rtmp_recv;
  237. conn->send[FIRSTSOCKET] = rtmp_send;
  238. return CURLE_OK;
  239. }
  240. static CURLcode rtmp_do(struct Curl_easy *data, bool *done)
  241. {
  242. struct connectdata *conn = data->conn;
  243. RTMP *r = conn->proto.rtmp;
  244. if(!RTMP_ConnectStream(r, 0))
  245. return CURLE_FAILED_INIT;
  246. if(data->state.upload) {
  247. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  248. Curl_xfer_setup1(data, CURL_XFER_SEND, -1, FALSE);
  249. }
  250. else
  251. Curl_xfer_setup1(data, CURL_XFER_RECV, -1, FALSE);
  252. *done = TRUE;
  253. return CURLE_OK;
  254. }
  255. static CURLcode rtmp_done(struct Curl_easy *data, CURLcode status,
  256. bool premature)
  257. {
  258. (void)data; /* unused */
  259. (void)status; /* unused */
  260. (void)premature; /* unused */
  261. return CURLE_OK;
  262. }
  263. static CURLcode rtmp_disconnect(struct Curl_easy *data,
  264. struct connectdata *conn,
  265. bool dead_connection)
  266. {
  267. RTMP *r = conn->proto.rtmp;
  268. (void)data;
  269. (void)dead_connection;
  270. if(r) {
  271. conn->proto.rtmp = NULL;
  272. RTMP_Close(r);
  273. RTMP_Free(r);
  274. }
  275. return CURLE_OK;
  276. }
  277. static ssize_t rtmp_recv(struct Curl_easy *data, int sockindex, char *buf,
  278. size_t len, CURLcode *err)
  279. {
  280. struct connectdata *conn = data->conn;
  281. RTMP *r = conn->proto.rtmp;
  282. ssize_t nread;
  283. (void)sockindex; /* unused */
  284. nread = RTMP_Read(r, buf, curlx_uztosi(len));
  285. if(nread < 0) {
  286. if(r->m_read.status == RTMP_READ_COMPLETE ||
  287. r->m_read.status == RTMP_READ_EOF) {
  288. data->req.size = data->req.bytecount;
  289. nread = 0;
  290. }
  291. else
  292. *err = CURLE_RECV_ERROR;
  293. }
  294. return nread;
  295. }
  296. static ssize_t rtmp_send(struct Curl_easy *data, int sockindex,
  297. const void *buf, size_t len, bool eos, CURLcode *err)
  298. {
  299. struct connectdata *conn = data->conn;
  300. RTMP *r = conn->proto.rtmp;
  301. ssize_t num;
  302. (void)sockindex; /* unused */
  303. (void)eos; /* unused */
  304. num = RTMP_Write(r, (char *)buf, curlx_uztosi(len));
  305. if(num < 0)
  306. *err = CURLE_SEND_ERROR;
  307. return num;
  308. }
  309. void Curl_rtmp_version(char *version, size_t len)
  310. {
  311. char suff[2];
  312. if(RTMP_LIB_VERSION & 0xff) {
  313. suff[0] = (RTMP_LIB_VERSION & 0xff) + 'a' - 1;
  314. suff[1] = '\0';
  315. }
  316. else
  317. suff[0] = '\0';
  318. msnprintf(version, len, "librtmp/%d.%d%s",
  319. RTMP_LIB_VERSION >> 16, (RTMP_LIB_VERSION >> 8) & 0xff,
  320. suff);
  321. }
  322. #endif /* USE_LIBRTMP */