371500e8a6a9f0d0134d44d26c32a7f71a8ae158
[libav.git] / libavformat / avio.c
1 /*
2 * unbuffered I/O
3 * Copyright (c) 2001 Fabrice Bellard
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 #include <unistd.h>
23
24 #include "libavutil/avstring.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/opt.h"
27 #include "os_support.h"
28 #include "avformat.h"
29 #if CONFIG_NETWORK
30 #include "network.h"
31 #endif
32 #include "url.h"
33
34 static URLProtocol *first_protocol = NULL;
35
36 URLProtocol *ffurl_protocol_next(URLProtocol *prev)
37 {
38 return prev ? prev->next : first_protocol;
39 }
40
41 /** @name Logging context. */
42 /*@{*/
43 static const char *urlcontext_to_name(void *ptr)
44 {
45 URLContext *h = (URLContext *)ptr;
46 if(h->prot) return h->prot->name;
47 else return "NULL";
48 }
49
50 static void *urlcontext_child_next(void *obj, void *prev)
51 {
52 URLContext *h = obj;
53 if (!prev && h->priv_data && h->prot->priv_data_class)
54 return h->priv_data;
55 return NULL;
56 }
57
58 static const AVClass *urlcontext_child_class_next(const AVClass *prev)
59 {
60 URLProtocol *p = NULL;
61
62 /* find the protocol that corresponds to prev */
63 while (prev && (p = ffurl_protocol_next(p)))
64 if (p->priv_data_class == prev)
65 break;
66
67 /* find next protocol with priv options */
68 while (p = ffurl_protocol_next(p))
69 if (p->priv_data_class)
70 return p->priv_data_class;
71 return NULL;
72
73 }
74
75 static const AVOption options[] = {{NULL}};
76 const AVClass ffurl_context_class = {
77 .class_name = "URLContext",
78 .item_name = urlcontext_to_name,
79 .option = options,
80 .version = LIBAVUTIL_VERSION_INT,
81 .child_next = urlcontext_child_next,
82 .child_class_next = urlcontext_child_class_next,
83 };
84 /*@}*/
85
86
87 const char *avio_enum_protocols(void **opaque, int output)
88 {
89 URLProtocol **p = opaque;
90 *p = ffurl_protocol_next(*p);
91 if (!*p) return NULL;
92 if ((output && (*p)->url_write) || (!output && (*p)->url_read))
93 return (*p)->name;
94 return avio_enum_protocols(opaque, output);
95 }
96
97 int ffurl_register_protocol(URLProtocol *protocol, int size)
98 {
99 URLProtocol **p;
100 if (size < sizeof(URLProtocol)) {
101 URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
102 memcpy(temp, protocol, size);
103 protocol = temp;
104 }
105 p = &first_protocol;
106 while (*p != NULL) p = &(*p)->next;
107 *p = protocol;
108 protocol->next = NULL;
109 return 0;
110 }
111
112 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
113 const char *filename, int flags,
114 const AVIOInterruptCB *int_cb)
115 {
116 URLContext *uc;
117 int err;
118
119 #if CONFIG_NETWORK
120 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
121 return AVERROR(EIO);
122 #endif
123 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
124 if (!uc) {
125 err = AVERROR(ENOMEM);
126 goto fail;
127 }
128 uc->av_class = &ffurl_context_class;
129 uc->filename = (char *) &uc[1];
130 strcpy(uc->filename, filename);
131 uc->prot = up;
132 uc->flags = flags;
133 uc->is_streamed = 0; /* default = not streamed */
134 uc->max_packet_size = 0; /* default: stream file */
135 if (up->priv_data_size) {
136 uc->priv_data = av_mallocz(up->priv_data_size);
137 if (up->priv_data_class) {
138 *(const AVClass**)uc->priv_data = up->priv_data_class;
139 av_opt_set_defaults(uc->priv_data);
140 }
141 }
142 if (int_cb)
143 uc->interrupt_callback = *int_cb;
144
145 *puc = uc;
146 return 0;
147 fail:
148 *puc = NULL;
149 #if CONFIG_NETWORK
150 if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
151 ff_network_close();
152 #endif
153 return err;
154 }
155
156 int ffurl_connect(URLContext* uc, AVDictionary **options)
157 {
158 int err =
159 uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
160 uc->prot->url_open(uc, uc->filename, uc->flags);
161 if (err)
162 return err;
163 uc->is_connected = 1;
164 //We must be careful here as ffurl_seek() could be slow, for example for http
165 if( (uc->flags & AVIO_FLAG_WRITE)
166 || !strcmp(uc->prot->name, "file"))
167 if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
168 uc->is_streamed= 1;
169 return 0;
170 }
171
172 #define URL_SCHEME_CHARS \
173 "abcdefghijklmnopqrstuvwxyz" \
174 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
175 "0123456789+-."
176
177 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
178 const AVIOInterruptCB *int_cb)
179 {
180 URLProtocol *up = NULL;
181 char proto_str[128], proto_nested[128], *ptr;
182 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
183
184 if (filename[proto_len] != ':' || is_dos_path(filename))
185 strcpy(proto_str, "file");
186 else
187 av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
188
189 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
190 if ((ptr = strchr(proto_nested, '+')))
191 *ptr = '\0';
192
193 while (up = ffurl_protocol_next(up)) {
194 if (!strcmp(proto_str, up->name))
195 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
196 if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
197 !strcmp(proto_nested, up->name))
198 return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
199 }
200 *puc = NULL;
201 return AVERROR(ENOENT);
202 }
203
204 int ffurl_open(URLContext **puc, const char *filename, int flags,
205 const AVIOInterruptCB *int_cb, AVDictionary **options)
206 {
207 int ret = ffurl_alloc(puc, filename, flags, int_cb);
208 if (ret)
209 return ret;
210 if (options && (*puc)->prot->priv_data_class &&
211 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
212 goto fail;
213 ret = ffurl_connect(*puc, options);
214 if (!ret)
215 return 0;
216 fail:
217 ffurl_close(*puc);
218 *puc = NULL;
219 return ret;
220 }
221
222 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
223 int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
224 {
225 int ret, len;
226 int fast_retries = 5;
227
228 len = 0;
229 while (len < size_min) {
230 ret = transfer_func(h, buf+len, size-len);
231 if (ret == AVERROR(EINTR))
232 continue;
233 if (h->flags & AVIO_FLAG_NONBLOCK)
234 return ret;
235 if (ret == AVERROR(EAGAIN)) {
236 ret = 0;
237 if (fast_retries)
238 fast_retries--;
239 else
240 usleep(1000);
241 } else if (ret < 1)
242 return ret < 0 ? ret : len;
243 if (ret)
244 fast_retries = FFMAX(fast_retries, 2);
245 len += ret;
246 if (ff_check_interrupt(&h->interrupt_callback))
247 return AVERROR_EXIT;
248 }
249 return len;
250 }
251
252 int ffurl_read(URLContext *h, unsigned char *buf, int size)
253 {
254 if (!(h->flags & AVIO_FLAG_READ))
255 return AVERROR(EIO);
256 return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
257 }
258
259 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
260 {
261 if (!(h->flags & AVIO_FLAG_READ))
262 return AVERROR(EIO);
263 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
264 }
265
266 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
267 {
268 if (!(h->flags & AVIO_FLAG_WRITE))
269 return AVERROR(EIO);
270 /* avoid sending too big packets */
271 if (h->max_packet_size && size > h->max_packet_size)
272 return AVERROR(EIO);
273
274 return retry_transfer_wrapper(h, buf, size, size, h->prot->url_write);
275 }
276
277 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
278 {
279 int64_t ret;
280
281 if (!h->prot->url_seek)
282 return AVERROR(ENOSYS);
283 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
284 return ret;
285 }
286
287 int ffurl_close(URLContext *h)
288 {
289 int ret = 0;
290 if (!h) return 0; /* can happen when ffurl_open fails */
291
292 if (h->is_connected && h->prot->url_close)
293 ret = h->prot->url_close(h);
294 #if CONFIG_NETWORK
295 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
296 ff_network_close();
297 #endif
298 if (h->prot->priv_data_size) {
299 if (h->prot->priv_data_class)
300 av_opt_free(h->priv_data);
301 av_free(h->priv_data);
302 }
303 av_free(h);
304 return ret;
305 }
306
307 int avio_check(const char *url, int flags)
308 {
309 URLContext *h;
310 int ret = ffurl_alloc(&h, url, flags, NULL);
311 if (ret)
312 return ret;
313
314 if (h->prot->url_check) {
315 ret = h->prot->url_check(h, flags);
316 } else {
317 ret = ffurl_connect(h, NULL);
318 if (ret >= 0)
319 ret = flags;
320 }
321
322 ffurl_close(h);
323 return ret;
324 }
325
326 int64_t ffurl_size(URLContext *h)
327 {
328 int64_t pos, size;
329
330 size= ffurl_seek(h, 0, AVSEEK_SIZE);
331 if(size<0){
332 pos = ffurl_seek(h, 0, SEEK_CUR);
333 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
334 return size;
335 size++;
336 ffurl_seek(h, pos, SEEK_SET);
337 }
338 return size;
339 }
340
341 int ffurl_get_file_handle(URLContext *h)
342 {
343 if (!h->prot->url_get_file_handle)
344 return -1;
345 return h->prot->url_get_file_handle(h);
346 }
347
348 int ffurl_shutdown(URLContext *h, int flags)
349 {
350 if (!h->prot->url_shutdown)
351 return AVERROR(EINVAL);
352 return h->prot->url_shutdown(h, flags);
353 }
354
355 int ff_check_interrupt(AVIOInterruptCB *cb)
356 {
357 int ret;
358 if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
359 return ret;
360 return 0;
361 }