curl_rtmp.c 14 KB

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