curl_rtmp.c 11 KB

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