2 * RTMP HTTP network protocol
3 * Copyright (c) 2012 Samuel Pitoiset
5 * This file is part of Libav.
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.
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.
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
27 #include "libavutil/avstring.h"
28 #include "libavutil/intfloat.h"
29 #include "libavutil/opt.h"
33 #define RTMPT_DEFAULT_PORT 80
35 /* protocol handler context */
36 typedef struct RTMP_HTTPContext {
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
49 static int rtmp_http_send_cmd(URLContext *h, const char *cmd)
51 RTMP_HTTPContext *rt = h->priv_data;
56 ff_url_join(uri, sizeof(uri), "http", NULL, rt->host, rt->port,
57 "/%s/%s/%d", cmd, rt->client_id, rt->seq++);
59 av_opt_set_bin(rt->stream->priv_data, "post_data", rt->out_data,
62 /* send a new request to the server */
63 if ((ret = ff_http_do_new_request(rt->stream, uri)) < 0)
66 /* re-init output buffer */
69 /* read the first byte which contains the polling interval */
70 if ((ret = ffurl_read(rt->stream, &c, 1)) < 0)
76 static int rtmp_http_write(URLContext *h, const uint8_t *buf, int size)
78 RTMP_HTTPContext *rt = h->priv_data;
81 if (rt->out_size + size > rt->out_capacity) {
82 rt->out_capacity = (rt->out_size + size) * 2;
83 ptr = av_realloc(rt->out_data, rt->out_capacity);
85 return AVERROR(ENOMEM);
89 memcpy(rt->out_data + rt->out_size, buf, size);
95 static int rtmp_http_read(URLContext *h, uint8_t *buf, int size)
97 RTMP_HTTPContext *rt = h->priv_data;
100 /* try to read at least 1 byte of data */
102 ret = ffurl_read(rt->stream, buf + off, size);
103 if (ret < 0 && ret != AVERROR_EOF)
106 if (ret == AVERROR_EOF) {
108 /* Do not send new requests when the client wants to
109 * close the connection. */
110 return AVERROR(EAGAIN);
113 /* When the client has reached end of file for the last request,
114 * we have to send a new request if we have buffered data.
115 * Otherwise, we have to send an idle POST. */
116 if (rt->out_size > 0) {
117 if ((ret = rtmp_http_send_cmd(h, "send")) < 0)
120 if ((ret = rtmp_http_write(h, "", 1)) < 0)
123 if ((ret = rtmp_http_send_cmd(h, "idle")) < 0)
127 if (h->flags & AVIO_FLAG_NONBLOCK) {
128 /* no incoming data to handle in nonblocking mode */
129 return AVERROR(EAGAIN);
140 static int rtmp_http_close(URLContext *h)
142 RTMP_HTTPContext *rt = h->priv_data;
143 uint8_t tmp_buf[2048];
146 if (rt->initialized) {
147 /* client wants to close the connection */
151 ret = rtmp_http_read(h, tmp_buf, sizeof(tmp_buf));
154 /* re-init output buffer before sending the close command */
157 if ((ret = rtmp_http_write(h, "", 1)) == 1)
158 ret = rtmp_http_send_cmd(h, "close");
161 av_freep(&rt->out_data);
162 ffurl_close(rt->stream);
167 static int rtmp_http_open(URLContext *h, const char *uri, int flags)
169 RTMP_HTTPContext *rt = h->priv_data;
170 char headers[1024], url[1024];
173 av_url_split(NULL, 0, NULL, 0, rt->host, sizeof(rt->host), &rt->port,
177 rt->port = RTMPT_DEFAULT_PORT;
179 /* This is the first request that is sent to the server in order to
180 * register a client on the server and start a new session. The server
181 * replies with a unique id (usually a number) that is used by the client
182 * for all future requests.
183 * Note: the reply doesn't contain a value for the polling interval.
184 * A successful connect resets the consecutive index that is used
186 ff_url_join(url, sizeof(url), "http", NULL, rt->host, rt->port, "/open/1");
188 /* alloc the http context */
189 if ((ret = ffurl_alloc(&rt->stream, url, AVIO_FLAG_READ_WRITE, NULL)) < 0)
193 snprintf(headers, sizeof(headers),
194 "Cache-Control: no-cache\r\n"
195 "Content-type: application/x-fcs\r\n"
196 "User-Agent: Shockwave Flash\r\n");
197 av_opt_set(rt->stream->priv_data, "headers", headers, 0);
198 av_opt_set(rt->stream->priv_data, "multiple_requests", "1", 0);
199 av_opt_set_bin(rt->stream->priv_data, "post_data", "", 1, 0);
201 /* open the http context */
202 if ((ret = ffurl_connect(rt->stream, NULL)) < 0)
205 /* read the server reply which contains a unique ID */
207 ret = ffurl_read(rt->stream, rt->client_id + off, sizeof(rt->client_id) - off);
208 if (ret == AVERROR_EOF)
213 if (off == sizeof(rt->client_id)) {
218 while (off > 0 && isspace(rt->client_id[off - 1]))
220 rt->client_id[off] = '\0';
222 /* http context is now initialized */
231 URLProtocol ff_rtmphttp_protocol = {
233 .url_open = rtmp_http_open,
234 .url_read = rtmp_http_read,
235 .url_write = rtmp_http_write,
236 .url_close = rtmp_http_close,
237 .priv_data_size = sizeof(RTMP_HTTPContext),
238 .flags = URL_PROTOCOL_FLAG_NETWORK,