curl_rtmp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 "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. ZERO_NULL, /* follow */
  79. PORT_RTMP, /* defport */
  80. CURLPROTO_RTMP, /* protocol */
  81. CURLPROTO_RTMP, /* family */
  82. PROTOPT_NONE /* flags */
  83. };
  84. const struct Curl_handler Curl_handler_rtmpt = {
  85. "rtmpt", /* scheme */
  86. rtmp_setup_connection, /* setup_connection */
  87. rtmp_do, /* do_it */
  88. rtmp_done, /* done */
  89. ZERO_NULL, /* do_more */
  90. rtmp_connect, /* connect_it */
  91. ZERO_NULL, /* connecting */
  92. ZERO_NULL, /* doing */
  93. ZERO_NULL, /* proto_getsock */
  94. ZERO_NULL, /* doing_getsock */
  95. ZERO_NULL, /* domore_getsock */
  96. ZERO_NULL, /* perform_getsock */
  97. rtmp_disconnect, /* disconnect */
  98. ZERO_NULL, /* write_resp */
  99. ZERO_NULL, /* write_resp_hd */
  100. ZERO_NULL, /* connection_check */
  101. ZERO_NULL, /* attach connection */
  102. ZERO_NULL, /* follow */
  103. PORT_RTMPT, /* defport */
  104. CURLPROTO_RTMPT, /* protocol */
  105. CURLPROTO_RTMPT, /* family */
  106. PROTOPT_NONE /* flags */
  107. };
  108. const struct Curl_handler Curl_handler_rtmpe = {
  109. "rtmpe", /* scheme */
  110. rtmp_setup_connection, /* setup_connection */
  111. rtmp_do, /* do_it */
  112. rtmp_done, /* done */
  113. ZERO_NULL, /* do_more */
  114. rtmp_connect, /* connect_it */
  115. ZERO_NULL, /* connecting */
  116. ZERO_NULL, /* doing */
  117. ZERO_NULL, /* proto_getsock */
  118. ZERO_NULL, /* doing_getsock */
  119. ZERO_NULL, /* domore_getsock */
  120. ZERO_NULL, /* perform_getsock */
  121. rtmp_disconnect, /* disconnect */
  122. ZERO_NULL, /* write_resp */
  123. ZERO_NULL, /* write_resp_hd */
  124. ZERO_NULL, /* connection_check */
  125. ZERO_NULL, /* attach connection */
  126. ZERO_NULL, /* follow */
  127. PORT_RTMP, /* defport */
  128. CURLPROTO_RTMPE, /* protocol */
  129. CURLPROTO_RTMPE, /* family */
  130. PROTOPT_NONE /* flags */
  131. };
  132. const struct Curl_handler Curl_handler_rtmpte = {
  133. "rtmpte", /* scheme */
  134. rtmp_setup_connection, /* setup_connection */
  135. rtmp_do, /* do_it */
  136. rtmp_done, /* done */
  137. ZERO_NULL, /* do_more */
  138. rtmp_connect, /* connect_it */
  139. ZERO_NULL, /* connecting */
  140. ZERO_NULL, /* doing */
  141. ZERO_NULL, /* proto_getsock */
  142. ZERO_NULL, /* doing_getsock */
  143. ZERO_NULL, /* domore_getsock */
  144. ZERO_NULL, /* perform_getsock */
  145. rtmp_disconnect, /* disconnect */
  146. ZERO_NULL, /* write_resp */
  147. ZERO_NULL, /* write_resp_hd */
  148. ZERO_NULL, /* connection_check */
  149. ZERO_NULL, /* attach connection */
  150. ZERO_NULL, /* follow */
  151. PORT_RTMPT, /* defport */
  152. CURLPROTO_RTMPTE, /* protocol */
  153. CURLPROTO_RTMPTE, /* family */
  154. PROTOPT_NONE /* flags */
  155. };
  156. const struct Curl_handler Curl_handler_rtmps = {
  157. "rtmps", /* scheme */
  158. rtmp_setup_connection, /* setup_connection */
  159. rtmp_do, /* do_it */
  160. rtmp_done, /* done */
  161. ZERO_NULL, /* do_more */
  162. rtmp_connect, /* connect_it */
  163. ZERO_NULL, /* connecting */
  164. ZERO_NULL, /* doing */
  165. ZERO_NULL, /* proto_getsock */
  166. ZERO_NULL, /* doing_getsock */
  167. ZERO_NULL, /* domore_getsock */
  168. ZERO_NULL, /* perform_getsock */
  169. rtmp_disconnect, /* disconnect */
  170. ZERO_NULL, /* write_resp */
  171. ZERO_NULL, /* write_resp_hd */
  172. ZERO_NULL, /* connection_check */
  173. ZERO_NULL, /* attach connection */
  174. ZERO_NULL, /* follow */
  175. PORT_RTMPS, /* defport */
  176. CURLPROTO_RTMPS, /* protocol */
  177. CURLPROTO_RTMP, /* family */
  178. PROTOPT_NONE /* flags */
  179. };
  180. const struct Curl_handler Curl_handler_rtmpts = {
  181. "rtmpts", /* scheme */
  182. rtmp_setup_connection, /* setup_connection */
  183. rtmp_do, /* do_it */
  184. rtmp_done, /* done */
  185. ZERO_NULL, /* do_more */
  186. rtmp_connect, /* connect_it */
  187. ZERO_NULL, /* connecting */
  188. ZERO_NULL, /* doing */
  189. ZERO_NULL, /* proto_getsock */
  190. ZERO_NULL, /* doing_getsock */
  191. ZERO_NULL, /* domore_getsock */
  192. ZERO_NULL, /* perform_getsock */
  193. rtmp_disconnect, /* disconnect */
  194. ZERO_NULL, /* write_resp */
  195. ZERO_NULL, /* write_resp_hd */
  196. ZERO_NULL, /* connection_check */
  197. ZERO_NULL, /* attach connection */
  198. ZERO_NULL, /* follow */
  199. PORT_RTMPS, /* defport */
  200. CURLPROTO_RTMPTS, /* protocol */
  201. CURLPROTO_RTMPT, /* family */
  202. PROTOPT_NONE /* flags */
  203. };
  204. static CURLcode rtmp_setup_connection(struct Curl_easy *data,
  205. struct connectdata *conn)
  206. {
  207. RTMP *r = RTMP_Alloc();
  208. if(!r)
  209. return CURLE_OUT_OF_MEMORY;
  210. RTMP_Init(r);
  211. RTMP_SetBufferMS(r, DEF_BUFTIME);
  212. if(!RTMP_SetupURL(r, data->state.url)) {
  213. RTMP_Free(r);
  214. return CURLE_URL_MALFORMAT;
  215. }
  216. conn->proto.rtmp = r;
  217. return CURLE_OK;
  218. }
  219. static CURLcode rtmp_connect(struct Curl_easy *data, bool *done)
  220. {
  221. struct connectdata *conn = data->conn;
  222. RTMP *r = conn->proto.rtmp;
  223. SET_RCVTIMEO(tv, 10);
  224. r->m_sb.sb_socket = (int)conn->sock[FIRSTSOCKET];
  225. /* We have to know if it is a write before we send the
  226. * connect request packet
  227. */
  228. if(data->state.upload)
  229. r->Link.protocol |= RTMP_FEATURE_WRITE;
  230. /* For plain streams, use the buffer toggle trick to keep data flowing */
  231. if(!(r->Link.lFlags & RTMP_LF_LIVE) &&
  232. !(r->Link.protocol & RTMP_FEATURE_HTTP))
  233. r->Link.lFlags |= RTMP_LF_BUFX;
  234. (void)curlx_nonblock(r->m_sb.sb_socket, FALSE);
  235. setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO,
  236. (char *)&tv, sizeof(tv));
  237. if(!RTMP_Connect1(r, NULL))
  238. return CURLE_FAILED_INIT;
  239. /* Clients must send a periodic BytesReceived report to the server */
  240. r->m_bSendCounter = TRUE;
  241. *done = TRUE;
  242. conn->recv[FIRSTSOCKET] = rtmp_recv;
  243. conn->send[FIRSTSOCKET] = rtmp_send;
  244. return CURLE_OK;
  245. }
  246. static CURLcode rtmp_do(struct Curl_easy *data, bool *done)
  247. {
  248. struct connectdata *conn = data->conn;
  249. RTMP *r = conn->proto.rtmp;
  250. if(!RTMP_ConnectStream(r, 0))
  251. return CURLE_FAILED_INIT;
  252. if(data->state.upload) {
  253. Curl_pgrsSetUploadSize(data, data->state.infilesize);
  254. Curl_xfer_setup1(data, CURL_XFER_SEND, -1, FALSE);
  255. }
  256. else
  257. Curl_xfer_setup1(data, CURL_XFER_RECV, -1, FALSE);
  258. *done = TRUE;
  259. return CURLE_OK;
  260. }
  261. static CURLcode rtmp_done(struct Curl_easy *data, CURLcode status,
  262. bool premature)
  263. {
  264. (void)data; /* unused */
  265. (void)status; /* unused */
  266. (void)premature; /* unused */
  267. return CURLE_OK;
  268. }
  269. static CURLcode rtmp_disconnect(struct Curl_easy *data,
  270. struct connectdata *conn,
  271. bool dead_connection)
  272. {
  273. RTMP *r = conn->proto.rtmp;
  274. (void)data;
  275. (void)dead_connection;
  276. if(r) {
  277. conn->proto.rtmp = NULL;
  278. RTMP_Close(r);
  279. RTMP_Free(r);
  280. }
  281. return CURLE_OK;
  282. }
  283. static ssize_t rtmp_recv(struct Curl_easy *data, int sockindex, char *buf,
  284. size_t len, CURLcode *err)
  285. {
  286. struct connectdata *conn = data->conn;
  287. RTMP *r = conn->proto.rtmp;
  288. ssize_t nread;
  289. (void)sockindex; /* unused */
  290. nread = RTMP_Read(r, buf, curlx_uztosi(len));
  291. if(nread < 0) {
  292. if(r->m_read.status == RTMP_READ_COMPLETE ||
  293. r->m_read.status == RTMP_READ_EOF) {
  294. data->req.size = data->req.bytecount;
  295. nread = 0;
  296. }
  297. else
  298. *err = CURLE_RECV_ERROR;
  299. }
  300. return nread;
  301. }
  302. static ssize_t rtmp_send(struct Curl_easy *data, int sockindex,
  303. const void *buf, size_t len, bool eos, CURLcode *err)
  304. {
  305. struct connectdata *conn = data->conn;
  306. RTMP *r = conn->proto.rtmp;
  307. ssize_t num;
  308. (void)sockindex; /* unused */
  309. (void)eos; /* unused */
  310. num = RTMP_Write(r, (char *)buf, curlx_uztosi(len));
  311. if(num < 0)
  312. *err = CURLE_SEND_ERROR;
  313. return num;
  314. }
  315. void Curl_rtmp_version(char *version, size_t len)
  316. {
  317. char suff[2];
  318. if(RTMP_LIB_VERSION & 0xff) {
  319. suff[0] = (RTMP_LIB_VERSION & 0xff) + 'a' - 1;
  320. suff[1] = '\0';
  321. }
  322. else
  323. suff[0] = '\0';
  324. msnprintf(version, len, "librtmp/%d.%d%s",
  325. RTMP_LIB_VERSION >> 16, (RTMP_LIB_VERSION >> 8) & 0xff,
  326. suff);
  327. }
  328. #endif /* USE_LIBRTMP */