]> git.sesse.net Git - ffmpeg/blob - libavformat/avio.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / avio.c
1 /*
2  * unbuffered I/O
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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(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 != NULL)
102         p = &(*p)->next;
103     *p             = protocol;
104     protocol->next = NULL;
105     return 0;
106 }
107
108 static int url_alloc_for_protocol(URLContext **puc, struct URLProtocol *up,
109                                   const char *filename, int flags,
110                                   const AVIOInterruptCB *int_cb)
111 {
112     URLContext *uc;
113     int err;
114
115 #if CONFIG_NETWORK
116     if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
117         return AVERROR(EIO);
118 #endif
119     if ((flags & AVIO_FLAG_READ) && !up->url_read) {
120         av_log(NULL, AV_LOG_ERROR,
121                "Impossible to open the '%s' protocol for reading\n", up->name);
122         return AVERROR(EIO);
123     }
124     if ((flags & AVIO_FLAG_WRITE) && !up->url_write) {
125         av_log(NULL, AV_LOG_ERROR,
126                "Impossible to open the '%s' protocol for writing\n", up->name);
127         return AVERROR(EIO);
128     }
129     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
130     if (!uc) {
131         err = AVERROR(ENOMEM);
132         goto fail;
133     }
134     uc->av_class = &ffurl_context_class;
135     uc->filename = (char *)&uc[1];
136     strcpy(uc->filename, filename);
137     uc->prot            = up;
138     uc->flags           = flags;
139     uc->is_streamed     = 0; /* default = not streamed */
140     uc->max_packet_size = 0; /* default: stream file */
141     if (up->priv_data_size) {
142         uc->priv_data = av_mallocz(up->priv_data_size);
143         if (!uc->priv_data) {
144             err = AVERROR(ENOMEM);
145             goto fail;
146         }
147         if (up->priv_data_class) {
148             int proto_len= strlen(up->name);
149             char *start = strchr(uc->filename, ',');
150             *(const AVClass **)uc->priv_data = up->priv_data_class;
151             av_opt_set_defaults(uc->priv_data);
152             if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
153                 int ret= 0;
154                 char *p= start;
155                 char sep= *++p;
156                 char *key, *val;
157                 p++;
158                 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
159                     *val= *key= 0;
160                     ret= av_opt_set(uc->priv_data, p, key+1, 0);
161                     if (ret == AVERROR_OPTION_NOT_FOUND)
162                         av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
163                     *val= *key= sep;
164                     p= val+1;
165                 }
166                 if(ret<0 || p!=key){
167                     av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
168                     av_freep(&uc->priv_data);
169                     av_freep(&uc);
170                     err = AVERROR(EINVAL);
171                     goto fail;
172                 }
173                 memmove(start, key+1, strlen(key));
174             }
175         }
176     }
177     if (int_cb)
178         uc->interrupt_callback = *int_cb;
179
180     *puc = uc;
181     return 0;
182 fail:
183     *puc = NULL;
184     if (uc)
185         av_freep(&uc->priv_data);
186     av_freep(&uc);
187 #if CONFIG_NETWORK
188     if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
189         ff_network_close();
190 #endif
191     return err;
192 }
193
194 int ffurl_connect(URLContext *uc, AVDictionary **options)
195 {
196     int err =
197         uc->prot->url_open2 ? uc->prot->url_open2(uc,
198                                                   uc->filename,
199                                                   uc->flags,
200                                                   options) :
201         uc->prot->url_open(uc, uc->filename, uc->flags);
202     if (err)
203         return err;
204     uc->is_connected = 1;
205     /* We must be careful here as ffurl_seek() could be slow,
206      * for example for http */
207     if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
208         if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
209             uc->is_streamed = 1;
210     return 0;
211 }
212
213 #define URL_SCHEME_CHARS                        \
214     "abcdefghijklmnopqrstuvwxyz"                \
215     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
216     "0123456789+-."
217
218 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
219                 const AVIOInterruptCB *int_cb)
220 {
221     URLProtocol *up = NULL;
222     char proto_str[128], proto_nested[128], *ptr;
223     size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
224
225     if (!first_protocol) {
226         av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
227                                      "Missing call to av_register_all()?\n");
228     }
229
230     if (filename[proto_len] != ':' &&
231         (filename[proto_len] != ',' || !strchr(filename + proto_len + 1, ':')) ||
232         is_dos_path(filename))
233         strcpy(proto_str, "file");
234     else
235         av_strlcpy(proto_str, filename,
236                    FFMIN(proto_len + 1, sizeof(proto_str)));
237
238     if ((ptr = strchr(proto_str, ',')))
239         *ptr = '\0';
240     av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
241     if ((ptr = strchr(proto_nested, '+')))
242         *ptr = '\0';
243
244     while (up = ffurl_protocol_next(up)) {
245         if (!strcmp(proto_str, up->name))
246             return url_alloc_for_protocol(puc, up, filename, flags, int_cb);
247         if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
248             !strcmp(proto_nested, up->name))
249             return url_alloc_for_protocol(puc, up, filename, flags, int_cb);
250     }
251     *puc = NULL;
252     if (!strcmp("https", proto_str))
253         av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile with openssl or gnutls enabled.\n");
254     return AVERROR_PROTOCOL_NOT_FOUND;
255 }
256
257 int ffurl_open(URLContext **puc, const char *filename, int flags,
258                const AVIOInterruptCB *int_cb, AVDictionary **options)
259 {
260     int ret = ffurl_alloc(puc, filename, flags, int_cb);
261     if (ret)
262         return ret;
263     if (options && (*puc)->prot->priv_data_class &&
264         (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
265         goto fail;
266     ret = ffurl_connect(*puc, options);
267     if (!ret)
268         return 0;
269 fail:
270     ffurl_close(*puc);
271     *puc = NULL;
272     return ret;
273 }
274
275 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
276                                          int size, int size_min,
277                                          int (*transfer_func)(URLContext *h,
278                                                               uint8_t *buf,
279                                                               int size))
280 {
281     int ret, len;
282     int fast_retries = 5;
283     int64_t wait_since = 0;
284
285     len = 0;
286     while (len < size_min) {
287         if (ff_check_interrupt(&h->interrupt_callback))
288             return AVERROR_EXIT;
289         ret = transfer_func(h, buf + len, size - len);
290         if (ret == AVERROR(EINTR))
291             continue;
292         if (h->flags & AVIO_FLAG_NONBLOCK)
293             return ret;
294         if (ret == AVERROR(EAGAIN)) {
295             ret = 0;
296             if (fast_retries) {
297                 fast_retries--;
298             } else {
299                 if (h->rw_timeout) {
300                     if (!wait_since)
301                         wait_since = av_gettime();
302                     else if (av_gettime() > wait_since + h->rw_timeout)
303                         return AVERROR(EIO);
304                 }
305                 av_usleep(1000);
306             }
307         } else if (ret < 1)
308             return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
309         if (ret)
310             fast_retries = FFMAX(fast_retries, 2);
311         len += ret;
312     }
313     return len;
314 }
315
316 int ffurl_read(URLContext *h, unsigned char *buf, int size)
317 {
318     if (!(h->flags & AVIO_FLAG_READ))
319         return AVERROR(EIO);
320     return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
321 }
322
323 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
324 {
325     if (!(h->flags & AVIO_FLAG_READ))
326         return AVERROR(EIO);
327     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
328 }
329
330 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
331 {
332     if (!(h->flags & AVIO_FLAG_WRITE))
333         return AVERROR(EIO);
334     /* avoid sending too big packets */
335     if (h->max_packet_size && size > h->max_packet_size)
336         return AVERROR(EIO);
337
338     return retry_transfer_wrapper(h, (unsigned char *)buf, size, size, (void*)h->prot->url_write);
339 }
340
341 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
342 {
343     int64_t ret;
344
345     if (!h->prot->url_seek)
346         return AVERROR(ENOSYS);
347     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
348     return ret;
349 }
350
351 int ffurl_closep(URLContext **hh)
352 {
353     URLContext *h= *hh;
354     int ret = 0;
355     if (!h)
356         return 0;     /* can happen when ffurl_open fails */
357
358     if (h->is_connected && h->prot->url_close)
359         ret = h->prot->url_close(h);
360 #if CONFIG_NETWORK
361     if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
362         ff_network_close();
363 #endif
364     if (h->prot->priv_data_size) {
365         if (h->prot->priv_data_class)
366             av_opt_free(h->priv_data);
367         av_freep(&h->priv_data);
368     }
369     av_freep(hh);
370     return ret;
371 }
372
373 int ffurl_close(URLContext *h)
374 {
375     return ffurl_closep(&h);
376 }
377
378
379 int avio_check(const char *url, int flags)
380 {
381     URLContext *h;
382     int ret = ffurl_alloc(&h, url, flags, NULL);
383     if (ret)
384         return ret;
385
386     if (h->prot->url_check) {
387         ret = h->prot->url_check(h, flags);
388     } else {
389         ret = ffurl_connect(h, NULL);
390         if (ret >= 0)
391             ret = flags;
392     }
393
394     ffurl_close(h);
395     return ret;
396 }
397
398 int64_t ffurl_size(URLContext *h)
399 {
400     int64_t pos, size;
401
402     size = ffurl_seek(h, 0, AVSEEK_SIZE);
403     if (size < 0) {
404         pos = ffurl_seek(h, 0, SEEK_CUR);
405         if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
406             return size;
407         size++;
408         ffurl_seek(h, pos, SEEK_SET);
409     }
410     return size;
411 }
412
413 int ffurl_get_file_handle(URLContext *h)
414 {
415     if (!h->prot->url_get_file_handle)
416         return -1;
417     return h->prot->url_get_file_handle(h);
418 }
419
420 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
421 {
422     if (!h->prot->url_get_multi_file_handle) {
423         if (!h->prot->url_get_file_handle)
424             return AVERROR(ENOSYS);
425         *handles = av_malloc(sizeof(**handles));
426         if (!*handles)
427             return AVERROR(ENOMEM);
428         *numhandles = 1;
429         *handles[0] = h->prot->url_get_file_handle(h);
430         return 0;
431     }
432     return h->prot->url_get_multi_file_handle(h, handles, numhandles);
433 }
434
435 int ffurl_shutdown(URLContext *h, int flags)
436 {
437     if (!h->prot->url_shutdown)
438         return AVERROR(EINVAL);
439     return h->prot->url_shutdown(h, flags);
440 }
441
442 int ff_check_interrupt(AVIOInterruptCB *cb)
443 {
444     int ret;
445     if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
446         return ret;
447     return 0;
448 }