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.

275 lines
8.6KB

  1. /*
  2. * RTMP HTTP network protocol
  3. * Copyright (c) 2012 Samuel Pitoiset
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. return err;
  80. }
  81. memcpy(rt->out_data + rt->out_size, buf, size);
  82. rt->out_size += size;
  83. return size;
  84. }
  85. static int rtmp_http_read(URLContext *h, uint8_t *buf, int size)
  86. {
  87. RTMP_HTTPContext *rt = h->priv_data;
  88. int ret, off = 0;
  89. /* try to read at least 1 byte of data */
  90. do {
  91. ret = ffurl_read(rt->stream, buf + off, size);
  92. if (ret < 0 && ret != AVERROR_EOF)
  93. return ret;
  94. if (ret == AVERROR_EOF) {
  95. if (rt->finishing) {
  96. /* Do not send new requests when the client wants to
  97. * close the connection. */
  98. return AVERROR(EAGAIN);
  99. }
  100. /* When the client has reached end of file for the last request,
  101. * we have to send a new request if we have buffered data.
  102. * Otherwise, we have to send an idle POST. */
  103. if (rt->out_size > 0) {
  104. if ((ret = rtmp_http_send_cmd(h, "send")) < 0)
  105. return ret;
  106. } else {
  107. if (rt->nb_bytes_read == 0) {
  108. /* Wait 50ms before retrying to read a server reply in
  109. * order to reduce the number of idle requets. */
  110. av_usleep(50000);
  111. }
  112. if ((ret = rtmp_http_write(h, "", 1)) < 0)
  113. return ret;
  114. if ((ret = rtmp_http_send_cmd(h, "idle")) < 0)
  115. return ret;
  116. }
  117. if (h->flags & AVIO_FLAG_NONBLOCK) {
  118. /* no incoming data to handle in nonblocking mode */
  119. return AVERROR(EAGAIN);
  120. }
  121. } else {
  122. off += ret;
  123. size -= ret;
  124. rt->nb_bytes_read += ret;
  125. }
  126. } while (off <= 0);
  127. return off;
  128. }
  129. static int rtmp_http_close(URLContext *h)
  130. {
  131. RTMP_HTTPContext *rt = h->priv_data;
  132. uint8_t tmp_buf[2048];
  133. int ret = 0;
  134. if (rt->initialized) {
  135. /* client wants to close the connection */
  136. rt->finishing = 1;
  137. do {
  138. ret = rtmp_http_read(h, tmp_buf, sizeof(tmp_buf));
  139. } while (ret > 0);
  140. /* re-init output buffer before sending the close command */
  141. rt->out_size = 0;
  142. if ((ret = rtmp_http_write(h, "", 1)) == 1)
  143. ret = rtmp_http_send_cmd(h, "close");
  144. }
  145. av_freep(&rt->out_data);
  146. ffurl_close(rt->stream);
  147. return ret;
  148. }
  149. static int rtmp_http_open(URLContext *h, const char *uri, int flags)
  150. {
  151. RTMP_HTTPContext *rt = h->priv_data;
  152. char headers[1024], url[1024];
  153. int ret, off = 0;
  154. av_url_split(NULL, 0, NULL, 0, rt->host, sizeof(rt->host), &rt->port,
  155. NULL, 0, uri);
  156. /* This is the first request that is sent to the server in order to
  157. * register a client on the server and start a new session. The server
  158. * replies with a unique id (usually a number) that is used by the client
  159. * for all future requests.
  160. * Note: the reply doesn't contain a value for the polling interval.
  161. * A successful connect resets the consecutive index that is used
  162. * in the URLs. */
  163. if (rt->tls) {
  164. if (rt->port < 0)
  165. rt->port = RTMPTS_DEFAULT_PORT;
  166. ff_url_join(url, sizeof(url), "https", NULL, rt->host, rt->port, "/open/1");
  167. } else {
  168. if (rt->port < 0)
  169. rt->port = RTMPT_DEFAULT_PORT;
  170. ff_url_join(url, sizeof(url), "http", NULL, rt->host, rt->port, "/open/1");
  171. }
  172. /* alloc the http context */
  173. if ((ret = ffurl_alloc(&rt->stream, url, AVIO_FLAG_READ_WRITE, NULL)) < 0)
  174. goto fail;
  175. /* set options */
  176. snprintf(headers, sizeof(headers),
  177. "Cache-Control: no-cache\r\n"
  178. "Content-type: application/x-fcs\r\n"
  179. "User-Agent: Shockwave Flash\r\n");
  180. av_opt_set(rt->stream->priv_data, "headers", headers, 0);
  181. av_opt_set(rt->stream->priv_data, "multiple_requests", "1", 0);
  182. av_opt_set_bin(rt->stream->priv_data, "post_data", "", 1, 0);
  183. /* open the http context */
  184. if ((ret = ffurl_connect(rt->stream, NULL)) < 0)
  185. goto fail;
  186. /* read the server reply which contains a unique ID */
  187. for (;;) {
  188. ret = ffurl_read(rt->stream, rt->client_id + off, sizeof(rt->client_id) - off);
  189. if (ret == AVERROR_EOF)
  190. break;
  191. if (ret < 0)
  192. goto fail;
  193. off += ret;
  194. if (off == sizeof(rt->client_id)) {
  195. ret = AVERROR(EIO);
  196. goto fail;
  197. }
  198. }
  199. while (off > 0 && av_isspace(rt->client_id[off - 1]))
  200. off--;
  201. rt->client_id[off] = '\0';
  202. /* http context is now initialized */
  203. rt->initialized = 1;
  204. return 0;
  205. fail:
  206. rtmp_http_close(h);
  207. return ret;
  208. }
  209. #define OFFSET(x) offsetof(RTMP_HTTPContext, x)
  210. #define DEC AV_OPT_FLAG_DECODING_PARAM
  211. static const AVOption ffrtmphttp_options[] = {
  212. {"ffrtmphttp_tls", "Use a HTTPS tunneling connection (RTMPTS).", OFFSET(tls), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC},
  213. { NULL },
  214. };
  215. static const AVClass ffrtmphttp_class = {
  216. .class_name = "ffrtmphttp",
  217. .item_name = av_default_item_name,
  218. .option = ffrtmphttp_options,
  219. .version = LIBAVUTIL_VERSION_INT,
  220. };
  221. URLProtocol ff_ffrtmphttp_protocol = {
  222. .name = "ffrtmphttp",
  223. .url_open = rtmp_http_open,
  224. .url_read = rtmp_http_read,
  225. .url_write = rtmp_http_write,
  226. .url_close = rtmp_http_close,
  227. .priv_data_size = sizeof(RTMP_HTTPContext),
  228. .flags = URL_PROTOCOL_FLAG_NETWORK,
  229. .priv_data_class= &ffrtmphttp_class,
  230. };