curl_rtmp.c 12 KB

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