544b49350007e2aa5ebeade22eda0bdff9686c50
[libav.git] / libavformat / rtmphttp.c
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 /**
23 * @file
24 * RTMP HTTP protocol
25 */
26
27 #include <unistd.h>
28
29 #include "libavutil/avstring.h"
30 #include "libavutil/intfloat.h"
31 #include "libavutil/opt.h"
32 #include "internal.h"
33 #include "http.h"
34
35 #define RTMPT_DEFAULT_PORT 80
36
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
50 } RTMP_HTTPContext;
51
52 static int rtmp_http_send_cmd(URLContext *h, const char *cmd)
53 {
54 RTMP_HTTPContext *rt = h->priv_data;
55 char uri[2048];
56 uint8_t c;
57 int ret;
58
59 ff_url_join(uri, sizeof(uri), "http", NULL, rt->host, rt->port,
60 "/%s/%s/%d", cmd, rt->client_id, rt->seq++);
61
62 av_opt_set_bin(rt->stream->priv_data, "post_data", rt->out_data,
63 rt->out_size, 0);
64
65 /* send a new request to the server */
66 if ((ret = ff_http_do_new_request(rt->stream, uri)) < 0)
67 return ret;
68
69 /* re-init output buffer */
70 rt->out_size = 0;
71
72 /* read the first byte which contains the polling interval */
73 if ((ret = ffurl_read(rt->stream, &c, 1)) < 0)
74 return ret;
75
76 /* re-init the number of bytes read */
77 rt->nb_bytes_read = 0;
78
79 return ret;
80 }
81
82 static int rtmp_http_write(URLContext *h, const uint8_t *buf, int size)
83 {
84 RTMP_HTTPContext *rt = h->priv_data;
85 void *ptr;
86
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);
90 if (!ptr)
91 return AVERROR(ENOMEM);
92 rt->out_data = ptr;
93 }
94
95 memcpy(rt->out_data + rt->out_size, buf, size);
96 rt->out_size += size;
97
98 return size;
99 }
100
101 static int rtmp_http_read(URLContext *h, uint8_t *buf, int size)
102 {
103 RTMP_HTTPContext *rt = h->priv_data;
104 int ret, off = 0;
105
106 /* try to read at least 1 byte of data */
107 do {
108 ret = ffurl_read(rt->stream, buf + off, size);
109 if (ret < 0 && ret != AVERROR_EOF)
110 return ret;
111
112 if (ret == AVERROR_EOF) {
113 if (rt->finishing) {
114 /* Do not send new requests when the client wants to
115 * close the connection. */
116 return AVERROR(EAGAIN);
117 }
118
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)
124 return ret;
125 } else {
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. */
129 usleep(50000);
130 }
131
132 if ((ret = rtmp_http_write(h, "", 1)) < 0)
133 return ret;
134
135 if ((ret = rtmp_http_send_cmd(h, "idle")) < 0)
136 return ret;
137 }
138
139 if (h->flags & AVIO_FLAG_NONBLOCK) {
140 /* no incoming data to handle in nonblocking mode */
141 return AVERROR(EAGAIN);
142 }
143 } else {
144 off += ret;
145 size -= ret;
146 rt->nb_bytes_read += ret;
147 }
148 } while (off <= 0);
149
150 return off;
151 }
152
153 static int rtmp_http_close(URLContext *h)
154 {
155 RTMP_HTTPContext *rt = h->priv_data;
156 uint8_t tmp_buf[2048];
157 int ret = 0;
158
159 if (rt->initialized) {
160 /* client wants to close the connection */
161 rt->finishing = 1;
162
163 do {
164 ret = rtmp_http_read(h, tmp_buf, sizeof(tmp_buf));
165 } while (ret > 0);
166
167 /* re-init output buffer before sending the close command */
168 rt->out_size = 0;
169
170 if ((ret = rtmp_http_write(h, "", 1)) == 1)
171 ret = rtmp_http_send_cmd(h, "close");
172 }
173
174 av_freep(&rt->out_data);
175 ffurl_close(rt->stream);
176
177 return ret;
178 }
179
180 static int rtmp_http_open(URLContext *h, const char *uri, int flags)
181 {
182 RTMP_HTTPContext *rt = h->priv_data;
183 char headers[1024], url[1024];
184 int ret, off = 0;
185
186 av_url_split(NULL, 0, NULL, 0, rt->host, sizeof(rt->host), &rt->port,
187 NULL, 0, uri);
188
189 if (rt->port < 0)
190 rt->port = RTMPT_DEFAULT_PORT;
191
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
198 * in the URLs. */
199 ff_url_join(url, sizeof(url), "http", NULL, rt->host, rt->port, "/open/1");
200
201 /* alloc the http context */
202 if ((ret = ffurl_alloc(&rt->stream, url, AVIO_FLAG_READ_WRITE, NULL)) < 0)
203 goto fail;
204
205 /* set options */
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);
213
214 /* open the http context */
215 if ((ret = ffurl_connect(rt->stream, NULL)) < 0)
216 goto fail;
217
218 /* read the server reply which contains a unique ID */
219 for (;;) {
220 ret = ffurl_read(rt->stream, rt->client_id + off, sizeof(rt->client_id) - off);
221 if (ret == AVERROR_EOF)
222 break;
223 if (ret < 0)
224 goto fail;
225 off += ret;
226 if (off == sizeof(rt->client_id)) {
227 ret = AVERROR(EIO);
228 goto fail;
229 }
230 }
231 while (off > 0 && isspace(rt->client_id[off - 1]))
232 off--;
233 rt->client_id[off] = '\0';
234
235 /* http context is now initialized */
236 rt->initialized = 1;
237 return 0;
238
239 fail:
240 rtmp_http_close(h);
241 return ret;
242 }
243
244 URLProtocol ff_rtmphttp_protocol = {
245 .name = "rtmphttp",
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,
252 };