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.

252 lines
7.8KB

  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. #define RTMPT_DEFAULT_PORT 80
  32. /* protocol handler context */
  33. typedef struct RTMP_HTTPContext {
  34. URLContext *stream; ///< HTTP stream
  35. char host[256]; ///< hostname of the server
  36. int port; ///< port to connect (default is 80)
  37. char client_id[64]; ///< client ID used for all requests except the first one
  38. int seq; ///< sequence ID used for all requests
  39. uint8_t *out_data; ///< output buffer
  40. int out_size; ///< current output buffer size
  41. int out_capacity; ///< current output buffer capacity
  42. int initialized; ///< flag indicating when the http context is initialized
  43. int finishing; ///< flag indicating when the client closes the connection
  44. int nb_bytes_read; ///< number of bytes read since the last request
  45. } RTMP_HTTPContext;
  46. static int rtmp_http_send_cmd(URLContext *h, const char *cmd)
  47. {
  48. RTMP_HTTPContext *rt = h->priv_data;
  49. char uri[2048];
  50. uint8_t c;
  51. int ret;
  52. ff_url_join(uri, sizeof(uri), "http", NULL, rt->host, rt->port,
  53. "/%s/%s/%d", cmd, rt->client_id, rt->seq++);
  54. av_opt_set_bin(rt->stream->priv_data, "post_data", rt->out_data,
  55. rt->out_size, 0);
  56. /* send a new request to the server */
  57. if ((ret = ff_http_do_new_request(rt->stream, uri)) < 0)
  58. return ret;
  59. /* re-init output buffer */
  60. rt->out_size = 0;
  61. /* read the first byte which contains the polling interval */
  62. if ((ret = ffurl_read(rt->stream, &c, 1)) < 0)
  63. return ret;
  64. /* re-init the number of bytes read */
  65. rt->nb_bytes_read = 0;
  66. return ret;
  67. }
  68. static int rtmp_http_write(URLContext *h, const uint8_t *buf, int size)
  69. {
  70. RTMP_HTTPContext *rt = h->priv_data;
  71. void *ptr;
  72. if (rt->out_size + size > rt->out_capacity) {
  73. rt->out_capacity = (rt->out_size + size) * 2;
  74. ptr = av_realloc(rt->out_data, rt->out_capacity);
  75. if (!ptr)
  76. return AVERROR(ENOMEM);
  77. rt->out_data = ptr;
  78. }
  79. memcpy(rt->out_data + rt->out_size, buf, size);
  80. rt->out_size += size;
  81. return size;
  82. }
  83. static int rtmp_http_read(URLContext *h, uint8_t *buf, int size)
  84. {
  85. RTMP_HTTPContext *rt = h->priv_data;
  86. int ret, off = 0;
  87. /* try to read at least 1 byte of data */
  88. do {
  89. ret = ffurl_read(rt->stream, buf + off, size);
  90. if (ret < 0 && ret != AVERROR_EOF)
  91. return ret;
  92. if (ret == AVERROR_EOF) {
  93. if (rt->finishing) {
  94. /* Do not send new requests when the client wants to
  95. * close the connection. */
  96. return AVERROR(EAGAIN);
  97. }
  98. /* When the client has reached end of file for the last request,
  99. * we have to send a new request if we have buffered data.
  100. * Otherwise, we have to send an idle POST. */
  101. if (rt->out_size > 0) {
  102. if ((ret = rtmp_http_send_cmd(h, "send")) < 0)
  103. return ret;
  104. } else {
  105. if (rt->nb_bytes_read == 0) {
  106. /* Wait 50ms before retrying to read a server reply in
  107. * order to reduce the number of idle requets. */
  108. av_usleep(50000);
  109. }
  110. if ((ret = rtmp_http_write(h, "", 1)) < 0)
  111. return ret;
  112. if ((ret = rtmp_http_send_cmd(h, "idle")) < 0)
  113. return ret;
  114. }
  115. if (h->flags & AVIO_FLAG_NONBLOCK) {
  116. /* no incoming data to handle in nonblocking mode */
  117. return AVERROR(EAGAIN);
  118. }
  119. } else {
  120. off += ret;
  121. size -= ret;
  122. rt->nb_bytes_read += ret;
  123. }
  124. } while (off <= 0);
  125. return off;
  126. }
  127. static int rtmp_http_close(URLContext *h)
  128. {
  129. RTMP_HTTPContext *rt = h->priv_data;
  130. uint8_t tmp_buf[2048];
  131. int ret = 0;
  132. if (rt->initialized) {
  133. /* client wants to close the connection */
  134. rt->finishing = 1;
  135. do {
  136. ret = rtmp_http_read(h, tmp_buf, sizeof(tmp_buf));
  137. } while (ret > 0);
  138. /* re-init output buffer before sending the close command */
  139. rt->out_size = 0;
  140. if ((ret = rtmp_http_write(h, "", 1)) == 1)
  141. ret = rtmp_http_send_cmd(h, "close");
  142. }
  143. av_freep(&rt->out_data);
  144. ffurl_close(rt->stream);
  145. return ret;
  146. }
  147. static int rtmp_http_open(URLContext *h, const char *uri, int flags)
  148. {
  149. RTMP_HTTPContext *rt = h->priv_data;
  150. char headers[1024], url[1024];
  151. int ret, off = 0;
  152. av_url_split(NULL, 0, NULL, 0, rt->host, sizeof(rt->host), &rt->port,
  153. NULL, 0, uri);
  154. if (rt->port < 0)
  155. rt->port = RTMPT_DEFAULT_PORT;
  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. ff_url_join(url, sizeof(url), "http", NULL, rt->host, rt->port, "/open/1");
  164. /* alloc the http context */
  165. if ((ret = ffurl_alloc(&rt->stream, url, AVIO_FLAG_READ_WRITE, NULL)) < 0)
  166. goto fail;
  167. /* set options */
  168. snprintf(headers, sizeof(headers),
  169. "Cache-Control: no-cache\r\n"
  170. "Content-type: application/x-fcs\r\n"
  171. "User-Agent: Shockwave Flash\r\n");
  172. av_opt_set(rt->stream->priv_data, "headers", headers, 0);
  173. av_opt_set(rt->stream->priv_data, "multiple_requests", "1", 0);
  174. av_opt_set_bin(rt->stream->priv_data, "post_data", "", 1, 0);
  175. /* open the http context */
  176. if ((ret = ffurl_connect(rt->stream, NULL)) < 0)
  177. goto fail;
  178. /* read the server reply which contains a unique ID */
  179. for (;;) {
  180. ret = ffurl_read(rt->stream, rt->client_id + off, sizeof(rt->client_id) - off);
  181. if (ret == AVERROR_EOF)
  182. break;
  183. if (ret < 0)
  184. goto fail;
  185. off += ret;
  186. if (off == sizeof(rt->client_id)) {
  187. ret = AVERROR(EIO);
  188. goto fail;
  189. }
  190. }
  191. while (off > 0 && isspace(rt->client_id[off - 1]))
  192. off--;
  193. rt->client_id[off] = '\0';
  194. /* http context is now initialized */
  195. rt->initialized = 1;
  196. return 0;
  197. fail:
  198. rtmp_http_close(h);
  199. return ret;
  200. }
  201. URLProtocol ff_rtmphttp_protocol = {
  202. .name = "rtmphttp",
  203. .url_open = rtmp_http_open,
  204. .url_read = rtmp_http_read,
  205. .url_write = rtmp_http_write,
  206. .url_close = rtmp_http_close,
  207. .priv_data_size = sizeof(RTMP_HTTPContext),
  208. .flags = URL_PROTOCOL_FLAG_NETWORK,
  209. };