2 * RTMP HTTP network protocol
3 * Copyright (c) 2012 Samuel Pitoiset
5 * This file is part of FFmpeg.
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.
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.
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
29 #include "libavutil/avstring.h"
30 #include "libavutil/intfloat.h"
31 #include "libavutil/opt.h"
35 #define RTMPT_DEFAULT_PORT 80
37 /* protocol handler context */
38 typedef struct RTMP_HTTPContext {
39 URLContext *stream; ///< HTTP stream
40 char host[256]; ///< hostname of the server
41 int port; ///< port to connect (default is 80)
42 char client_id[64]; ///< client ID used for all requests except the first one
43 int seq; ///< sequence ID used for all requests
44 uint8_t *out_data; ///< output buffer
45 int out_size; ///< current output buffer size
46 int out_capacity; ///< current output buffer capacity
47 int initialized; ///< flag indicating when the http context is initialized
48 int finishing; ///< flag indicating when the client closes the connection
49 int nb_bytes_read; ///< number of bytes read since the last request
52 static int rtmp_http_send_cmd(URLContext *h, const char *cmd)
54 RTMP_HTTPContext *rt = h->priv_data;
59 ff_url_join(uri, sizeof(uri), "http", NULL, rt->host, rt->port,
60 "/%s/%s/%d", cmd, rt->client_id, rt->seq++);
62 av_opt_set_bin(rt->stream->priv_data, "post_data", rt->out_data,
65 /* send a new request to the server */
66 if ((ret = ff_http_do_new_request(rt->stream, uri)) < 0)
69 /* re-init output buffer */
72 /* read the first byte which contains the polling interval */
73 if ((ret = ffurl_read(rt->stream, &c, 1)) < 0)
76 /* re-init the number of bytes read */
77 rt->nb_bytes_read = 0;
82 static int rtmp_http_write(URLContext *h, const uint8_t *buf, int size)
84 RTMP_HTTPContext *rt = h->priv_data;
87 if (rt->out_size + size > rt->out_capacity) {
88 rt->out_capacity = (rt->out_size + size) * 2;
89 ptr = av_realloc(rt->out_data, rt->out_capacity);
91 return AVERROR(ENOMEM);
95 memcpy(rt->out_data + rt->out_size, buf, size);
101 static int rtmp_http_read(URLContext *h, uint8_t *buf, int size)
103 RTMP_HTTPContext *rt = h->priv_data;
106 /* try to read at least 1 byte of data */
108 ret = ffurl_read(rt->stream, buf + off, size);
109 if (ret < 0 && ret != AVERROR_EOF)
112 if (ret == AVERROR_EOF) {
114 /* Do not send new requests when the client wants to
115 * close the connection. */
116 return AVERROR(EAGAIN);
119 /* When the client has reached end of file for the last request,
120 * we have to send a new request if we have buffered data.
121 * Otherwise, we have to send an idle POST. */
122 if (rt->out_size > 0) {
123 if ((ret = rtmp_http_send_cmd(h, "send")) < 0)
126 if (rt->nb_bytes_read == 0) {
127 /* Wait 50ms before retrying to read a server reply in
128 * order to reduce the number of idle requets. */
132 if ((ret = rtmp_http_write(h, "", 1)) < 0)
135 if ((ret = rtmp_http_send_cmd(h, "idle")) < 0)
139 if (h->flags & AVIO_FLAG_NONBLOCK) {
140 /* no incoming data to handle in nonblocking mode */
141 return AVERROR(EAGAIN);
146 rt->nb_bytes_read += ret;
153 static int rtmp_http_close(URLContext *h)
155 RTMP_HTTPContext *rt = h->priv_data;
156 uint8_t tmp_buf[2048];
159 if (rt->initialized) {
160 /* client wants to close the connection */
164 ret = rtmp_http_read(h, tmp_buf, sizeof(tmp_buf));
167 /* re-init output buffer before sending the close command */
170 if ((ret = rtmp_http_write(h, "", 1)) == 1)
171 ret = rtmp_http_send_cmd(h, "close");
174 av_freep(&rt->out_data);
175 ffurl_close(rt->stream);
180 static int rtmp_http_open(URLContext *h, const char *uri, int flags)
182 RTMP_HTTPContext *rt = h->priv_data;
183 char headers[1024], url[1024];
186 av_url_split(NULL, 0, NULL, 0, rt->host, sizeof(rt->host), &rt->port,
190 rt->port = RTMPT_DEFAULT_PORT;
192 /* This is the first request that is sent to the server in order to
193 * register a client on the server and start a new session. The server
194 * replies with a unique id (usually a number) that is used by the client
195 * for all future requests.
196 * Note: the reply doesn't contain a value for the polling interval.
197 * A successful connect resets the consecutive index that is used
199 ff_url_join(url, sizeof(url), "http", NULL, rt->host, rt->port, "/open/1");
201 /* alloc the http context */
202 if ((ret = ffurl_alloc(&rt->stream, url, AVIO_FLAG_READ_WRITE, NULL)) < 0)
206 snprintf(headers, sizeof(headers),
207 "Cache-Control: no-cache\r\n"
208 "Content-type: application/x-fcs\r\n"
209 "User-Agent: Shockwave Flash\r\n");
210 av_opt_set(rt->stream->priv_data, "headers", headers, 0);
211 av_opt_set(rt->stream->priv_data, "multiple_requests", "1", 0);
212 av_opt_set_bin(rt->stream->priv_data, "post_data", "", 1, 0);
214 /* open the http context */
215 if ((ret = ffurl_connect(rt->stream, NULL)) < 0)
218 /* read the server reply which contains a unique ID */
220 ret = ffurl_read(rt->stream, rt->client_id + off, sizeof(rt->client_id) - off);
221 if (ret == AVERROR_EOF)
226 if (off == sizeof(rt->client_id)) {
231 while (off > 0 && isspace(rt->client_id[off - 1]))
233 rt->client_id[off] = '\0';
235 /* http context is now initialized */
244 URLProtocol ff_rtmphttp_protocol = {
246 .url_open = rtmp_http_open,
247 .url_read = rtmp_http_read,
248 .url_write = rtmp_http_write,
249 .url_close = rtmp_http_close,
250 .priv_data_size = sizeof(RTMP_HTTPContext),
251 .flags = URL_PROTOCOL_FLAG_NETWORK,