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.

240 lines
7.3KB

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