curl_rtmp.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2010, Howard Chu, <hyc@highlandsun.com>
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "setup.h"
  23. #ifdef USE_LIBRTMP
  24. #include "urldata.h"
  25. #include "nonblock.h" /* for curlx_nonblock */
  26. #include "progress.h" /* for Curl_pgrsSetUploadSize */
  27. #include "transfer.h"
  28. #include <curl/curl.h>
  29. #include <librtmp/rtmp.h>
  30. #define _MPRINTF_REPLACE /* use our functions only */
  31. #include <curl/mprintf.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(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_connection);
  47. static Curl_recv rtmp_recv;
  48. static Curl_send rtmp_send;
  49. /*
  50. * RTMP protocol handler.h, based on http://rtmpdump.mplayerhq.hu
  51. */
  52. const struct Curl_handler Curl_handler_rtmp = {
  53. "RTMP", /* scheme */
  54. rtmp_setup, /* 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, /* perform_getsock */
  64. rtmp_disconnect, /* disconnect */
  65. PORT_RTMP, /* defport */
  66. PROT_RTMP /* protocol */
  67. };
  68. const struct Curl_handler Curl_handler_rtmpt = {
  69. "RTMPT", /* scheme */
  70. rtmp_setup, /* setup_connection */
  71. rtmp_do, /* do_it */
  72. rtmp_done, /* done */
  73. ZERO_NULL, /* do_more */
  74. rtmp_connect, /* connect_it */
  75. ZERO_NULL, /* connecting */
  76. ZERO_NULL, /* doing */
  77. ZERO_NULL, /* proto_getsock */
  78. ZERO_NULL, /* doing_getsock */
  79. ZERO_NULL, /* perform_getsock */
  80. rtmp_disconnect, /* disconnect */
  81. PORT_RTMPT, /* defport */
  82. PROT_RTMPT /* protocol */
  83. };
  84. const struct Curl_handler Curl_handler_rtmpe = {
  85. "RTMPE", /* scheme */
  86. rtmp_setup, /* 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, /* perform_getsock */
  96. rtmp_disconnect, /* disconnect */
  97. PORT_RTMP, /* defport */
  98. PROT_RTMPE /* protocol */
  99. };
  100. const struct Curl_handler Curl_handler_rtmpte = {
  101. "RTMPTE", /* scheme */
  102. rtmp_setup, /* setup_connection */
  103. rtmp_do, /* do_it */
  104. rtmp_done, /* done */
  105. ZERO_NULL, /* do_more */
  106. rtmp_connect, /* connect_it */
  107. ZERO_NULL, /* connecting */
  108. ZERO_NULL, /* doing */
  109. ZERO_NULL, /* proto_getsock */
  110. ZERO_NULL, /* doing_getsock */
  111. ZERO_NULL, /* perform_getsock */
  112. rtmp_disconnect, /* disconnect */
  113. PORT_RTMPT, /* defport */
  114. PROT_RTMPTE /* protocol */
  115. };
  116. const struct Curl_handler Curl_handler_rtmps = {
  117. "RTMPS", /* scheme */
  118. rtmp_setup, /* setup_connection */
  119. rtmp_do, /* do_it */
  120. rtmp_done, /* done */
  121. ZERO_NULL, /* do_more */
  122. rtmp_connect, /* connect_it */
  123. ZERO_NULL, /* connecting */
  124. ZERO_NULL, /* doing */
  125. ZERO_NULL, /* proto_getsock */
  126. ZERO_NULL, /* doing_getsock */
  127. ZERO_NULL, /* perform_getsock */
  128. rtmp_disconnect, /* disconnect */
  129. PORT_RTMPS, /* defport */
  130. PROT_RTMPS /* protocol */
  131. };
  132. const struct Curl_handler Curl_handler_rtmpts = {
  133. "RTMPTS", /* scheme */
  134. rtmp_setup, /* 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, /* perform_getsock */
  144. rtmp_disconnect, /* disconnect */
  145. PORT_RTMPS, /* defport */
  146. PROT_RTMPTS /* protocol */
  147. };
  148. static CURLcode rtmp_setup(struct connectdata *conn)
  149. {
  150. RTMP *r = RTMP_Alloc();
  151. if (!r)
  152. return CURLE_OUT_OF_MEMORY;
  153. RTMP_Init(r);
  154. RTMP_SetBufferMS(r, DEF_BUFTIME);
  155. if (!RTMP_SetupURL(r, conn->data->change.url)) {
  156. RTMP_Free(r);
  157. return CURLE_URL_MALFORMAT;
  158. }
  159. conn->proto.generic = r;
  160. return CURLE_OK;
  161. }
  162. static CURLcode rtmp_connect(struct connectdata *conn, bool *done)
  163. {
  164. RTMP *r = conn->proto.generic;
  165. SET_RCVTIMEO(tv,10);
  166. r->m_sb.sb_socket = conn->sock[FIRSTSOCKET];
  167. /* We have to know if it's a write before we send the
  168. * connect request packet
  169. */
  170. if (conn->data->set.upload)
  171. r->Link.protocol |= RTMP_FEATURE_WRITE;
  172. /* For plain streams, use the buffer toggle trick to keep data flowing */
  173. if (!(r->Link.lFlags & RTMP_LF_LIVE) && !(r->Link.protocol & RTMP_FEATURE_HTTP))
  174. r->Link.lFlags |= RTMP_LF_BUFX;
  175. curlx_nonblock(r->m_sb.sb_socket, FALSE);
  176. setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
  177. if (!RTMP_Connect1(r, NULL))
  178. return CURLE_FAILED_INIT;
  179. /* Clients must send a periodic BytesReceived report to the server */
  180. r->m_bSendCounter = true;
  181. *done = TRUE;
  182. conn->recv[FIRSTSOCKET] = rtmp_recv;
  183. conn->send[FIRSTSOCKET] = rtmp_send;
  184. return CURLE_OK;
  185. }
  186. static CURLcode rtmp_do(struct connectdata *conn, bool *done)
  187. {
  188. RTMP *r = conn->proto.generic;
  189. if (!RTMP_ConnectStream(r, 0))
  190. return CURLE_FAILED_INIT;
  191. if (conn->data->set.upload) {
  192. Curl_pgrsSetUploadSize(conn->data, conn->data->set.infilesize);
  193. Curl_setup_transfer(conn, -1, -1, FALSE, NULL, FIRSTSOCKET, NULL);
  194. } else
  195. Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
  196. *done = TRUE;
  197. return CURLE_OK;
  198. }
  199. static CURLcode rtmp_done(struct connectdata *conn, CURLcode status,
  200. bool premature)
  201. {
  202. (void)conn; /* unused */
  203. (void)status; /* unused */
  204. (void)premature; /* unused */
  205. return CURLE_OK;
  206. }
  207. static CURLcode rtmp_disconnect(struct connectdata *conn,
  208. bool dead_connection)
  209. {
  210. RTMP *r = conn->proto.generic;
  211. (void)dead_connection;
  212. if (r) {
  213. conn->proto.generic = NULL;
  214. RTMP_Close(r);
  215. RTMP_Free(r);
  216. }
  217. return CURLE_OK;
  218. }
  219. static ssize_t rtmp_recv(struct connectdata *conn, int sockindex, char *buf,
  220. size_t len, CURLcode *err)
  221. {
  222. RTMP *r = conn->proto.generic;
  223. ssize_t nread;
  224. (void)sockindex; /* unused */
  225. nread = RTMP_Read(r, buf, len);
  226. if (nread < 0) {
  227. if (r->m_read.status == RTMP_READ_COMPLETE ||
  228. r->m_read.status == RTMP_READ_EOF) {
  229. conn->data->req.size = conn->data->req.bytecount;
  230. nread = 0;
  231. } else
  232. *err = CURLE_RECV_ERROR;
  233. }
  234. return nread;
  235. }
  236. static ssize_t rtmp_send(struct connectdata *conn, int sockindex,
  237. const void *buf, size_t len, CURLcode *err)
  238. {
  239. RTMP *r = conn->proto.generic;
  240. ssize_t num;
  241. (void)sockindex; /* unused */
  242. num = RTMP_Write(r, (char *)buf, len);
  243. if (num < 0) {
  244. *err = CURLE_SEND_ERROR;
  245. }
  246. return num;
  247. }
  248. #endif /* USE_LIBRTMP */