You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
8.7KB

  1. /*
  2. * RTMP HTTP network protocol
  3. * Copyright (c) 2012 Samuel Pitoiset
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * RTMP HTTP protocol
  24. */
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/intfloat.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/time.h"
  29. #include "internal.h"
  30. #include "http.h"
  31. #include "rtmp.h"
  32. #define RTMPT_DEFAULT_PORT 80
  33. #define RTMPTS_DEFAULT_PORT RTMPS_DEFAULT_PORT
  34. /* protocol handler context */
  35. typedef struct RTMP_HTTPContext {
  36. const AVClass *class;
  37. URLContext *stream; ///< HTTP stream
  38. char host[256]; ///< hostname of the server
  39. int port; ///< port to connect (default is 80)
  40. char client_id[64]; ///< client ID used for all requests except the first one
  41. int seq; ///< sequence ID used for all requests
  42. uint8_t *out_data; ///< output buffer
  43. int out_size; ///< current output buffer size
  44. int out_capacity; ///< current output buffer capacity
  45. int initialized; ///< flag indicating when the http context is initialized
  46. int finishing; ///< flag indicating when the client closes the connection
  47. int nb_bytes_read; ///< number of bytes read since the last request
  48. int tls; ///< use Transport Security Layer (RTMPTS)
  49. } RTMP_HTTPContext;
  50. static int rtmp_http_send_cmd(URLContext *h, const char *cmd)
  51. {
  52. RTMP_HTTPContext *rt = h->priv_data;
  53. char uri[2048];
  54. uint8_t c;
  55. int ret;
  56. ff_url_join(uri, sizeof(uri), "http", NULL, rt->host, rt->port,
  57. "/%s/%s/%d", cmd, rt->client_id, rt->seq++);
  58. av_opt_set_bin(rt->stream->priv_data, "post_data", rt->out_data,
  59. rt->out_size, 0);
  60. /* send a new request to the server */
  61. if ((ret = ff_http_do_new_request(rt->stream, uri)) < 0)
  62. return ret;
  63. /* re-init output buffer */
  64. rt->out_size = 0;
  65. /* read the first byte which contains the polling interval */
  66. if ((ret = ffurl_read(rt->stream, &c, 1)) < 0)
  67. return ret;
  68. /* re-init the number of bytes read */
  69. rt->nb_bytes_read = 0;
  70. return ret;
  71. }
  72. static int rtmp_http_write(URLContext *h, const uint8_t *buf, int size)
  73. {
  74. RTMP_HTTPContext *rt = h->priv_data;
  75. if (rt->out_size + size > rt->out_capacity) {
  76. int err;
  77. rt->out_capacity = (rt->out_size + size) * 2;
  78. if ((err = av_reallocp(&rt->out_data, rt->out_capacity)) < 0) {
  79. rt->out_size = 0;
  80. rt->out_capacity = 0;
  81. return err;
  82. }
  83. }
  84. memcpy(rt->out_data + rt->out_size, buf, size);
  85. rt->out_size += size;
  86. return size;
  87. }
  88. static int rtmp_http_read(URLContext *h, uint8_t *buf, int size)
  89. {
  90. RTMP_HTTPContext *rt = h->priv_data;
  91. int ret, off = 0;
  92. /* try to read at least 1 byte of data */
  93. do {
  94. ret = ffurl_read(rt->stream, buf + off, size);
  95. if (ret < 0 && ret != AVERROR_EOF)
  96. return ret;
  97. if (!ret || ret == AVERROR_EOF) {
  98. if (rt->finishing) {
  99. /* Do not send new requests when the client wants to
  100. * close the connection. */
  101. return AVERROR(EAGAIN);
  102. }
  103. /* When the client has reached end of file for the last request,
  104. * we have to send a new request if we have buffered data.
  105. * Otherwise, we have to send an idle POST. */
  106. if (rt->out_size > 0) {
  107. if ((ret = rtmp_http_send_cmd(h, "send")) < 0)
  108. return ret;
  109. } else {
  110. if (rt->nb_bytes_read == 0) {
  111. /* Wait 50ms before retrying to read a server reply in
  112. * order to reduce the number of idle requests. */
  113. av_usleep(50000);
  114. }
  115. if ((ret = rtmp_http_write(h, "", 1)) < 0)
  116. return ret;
  117. if ((ret = rtmp_http_send_cmd(h, "idle")) < 0)
  118. return ret;
  119. }
  120. if (h->flags & AVIO_FLAG_NONBLOCK) {
  121. /* no incoming data to handle in nonblocking mode */
  122. return AVERROR(EAGAIN);
  123. }
  124. } else {
  125. off += ret;
  126. size -= ret;
  127. rt->nb_bytes_read += ret;
  128. }
  129. } while (off <= 0);
  130. return off;
  131. }
  132. static int rtmp_http_close(URLContext *h)
  133. {
  134. RTMP_HTTPContext *rt = h->priv_data;
  135. uint8_t tmp_buf[2048];
  136. int ret = 0;
  137. if (rt->initialized) {
  138. /* client wants to close the connection */
  139. rt->finishing = 1;
  140. do {
  141. ret = rtmp_http_read(h, tmp_buf, sizeof(tmp_buf));
  142. } while (ret > 0);
  143. /* re-init output buffer before sending the close command */
  144. rt->out_size = 0;
  145. if ((ret = rtmp_http_write(h, "", 1)) == 1)
  146. ret = rtmp_http_send_cmd(h, "close");
  147. }
  148. av_freep(&rt->out_data);
  149. ffurl_close(rt->stream);
  150. return ret;
  151. }
  152. static int rtmp_http_open(URLContext *h, const char *uri, int flags)
  153. {
  154. RTMP_HTTPContext *rt = h->priv_data;
  155. char headers[1024], url[1024];
  156. int ret, off = 0;
  157. av_url_split(NULL, 0, NULL, 0, rt->host, sizeof(rt->host), &rt->port,
  158. NULL, 0, uri);
  159. /* This is the first request that is sent to the server in order to
  160. * register a client on the server and start a new session. The server
  161. * replies with a unique id (usually a number) that is used by the client
  162. * for all future requests.
  163. * Note: the reply doesn't contain a value for the polling interval.
  164. * A successful connect resets the consecutive index that is used
  165. * in the URLs. */
  166. if (rt->tls) {
  167. if (rt->port < 0)
  168. rt->port = RTMPTS_DEFAULT_PORT;
  169. ff_url_join(url, sizeof(url), "https", NULL, rt->host, rt->port, "/open/1");
  170. } else {
  171. if (rt->port < 0)
  172. rt->port = RTMPT_DEFAULT_PORT;
  173. ff_url_join(url, sizeof(url), "http", NULL, rt->host, rt->port, "/open/1");
  174. }
  175. /* alloc the http context */
  176. if ((ret = ffurl_alloc(&rt->stream, url, AVIO_FLAG_READ_WRITE, NULL, h->protocols)) < 0)
  177. goto fail;
  178. /* set options */
  179. snprintf(headers, sizeof(headers),
  180. "Cache-Control: no-cache\r\n"
  181. "Content-type: application/x-fcs\r\n"
  182. "User-Agent: Shockwave Flash\r\n");
  183. av_opt_set(rt->stream->priv_data, "headers", headers, 0);
  184. av_opt_set(rt->stream->priv_data, "multiple_requests", "1", 0);
  185. av_opt_set_bin(rt->stream->priv_data, "post_data", "", 1, 0);
  186. /* open the http context */
  187. if ((ret = ffurl_connect(rt->stream, NULL)) < 0)
  188. goto fail;
  189. /* read the server reply which contains a unique ID */
  190. for (;;) {
  191. ret = ffurl_read(rt->stream, rt->client_id + off, sizeof(rt->client_id) - off);
  192. if (!ret || ret == AVERROR_EOF)
  193. break;
  194. if (ret < 0)
  195. goto fail;
  196. off += ret;
  197. if (off == sizeof(rt->client_id)) {
  198. ret = AVERROR(EIO);
  199. goto fail;
  200. }
  201. }
  202. while (off > 0 && av_isspace(rt->client_id[off - 1]))
  203. off--;
  204. rt->client_id[off] = '\0';
  205. /* http context is now initialized */
  206. rt->initialized = 1;
  207. return 0;
  208. fail:
  209. rtmp_http_close(h);
  210. return ret;
  211. }
  212. #define OFFSET(x) offsetof(RTMP_HTTPContext, x)
  213. #define DEC AV_OPT_FLAG_DECODING_PARAM
  214. static const AVOption ffrtmphttp_options[] = {
  215. {"ffrtmphttp_tls", "Use a HTTPS tunneling connection (RTMPTS).", OFFSET(tls), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC},
  216. { NULL },
  217. };
  218. static const AVClass ffrtmphttp_class = {
  219. .class_name = "ffrtmphttp",
  220. .item_name = av_default_item_name,
  221. .option = ffrtmphttp_options,
  222. .version = LIBAVUTIL_VERSION_INT,
  223. };
  224. const URLProtocol ff_ffrtmphttp_protocol = {
  225. .name = "ffrtmphttp",
  226. .url_open = rtmp_http_open,
  227. .url_read = rtmp_http_read,
  228. .url_write = rtmp_http_write,
  229. .url_close = rtmp_http_close,
  230. .priv_data_size = sizeof(RTMP_HTTPContext),
  231. .flags = URL_PROTOCOL_FLAG_NETWORK,
  232. .priv_data_class= &ffrtmphttp_class,
  233. };