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