curl_rtmp.c 13 KB

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