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