curl_rtmp.c 13 KB

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