]> 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 static struct URLProtocol *url_find_protocol(const char *filename)
219 {
220     URLProtocol *up = NULL;
221     char proto_str[128], proto_nested[128], *ptr;
222     size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
223
224     if (filename[proto_len] != ':' &&
225         (filename[proto_len] != ',' || !strchr(filename + proto_len + 1, ':')) ||
226         is_dos_path(filename))
227         strcpy(proto_str, "file");
228     else
229         av_strlcpy(proto_str, filename,
230                    FFMIN(proto_len + 1, sizeof(proto_str)));
231
232     if ((ptr = strchr(proto_str, ',')))
233         *ptr = '\0';
234     av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
235     if ((ptr = strchr(proto_nested, '+')))
236         *ptr = '\0';
237
238     while (up = ffurl_protocol_next(up)) {
239         if (!strcmp(proto_str, up->name))
240             break;
241         if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
242             !strcmp(proto_nested, up->name))
243             break;
244     }
245
246     return up;
247 }
248
249 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
250                 const AVIOInterruptCB *int_cb)
251 {
252     URLProtocol *p = NULL;
253
254     if (!first_protocol) {
255         av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
256                                      "Missing call to av_register_all()?\n");
257     }
258
259     p = url_find_protocol(filename);
260     if (p)
261        return url_alloc_for_protocol(puc, p, filename, flags, int_cb);
262
263     *puc = NULL;
264     if (av_strstart("https:", filename, NULL))
265         av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile with openssl or gnutls enabled.\n");
266     return AVERROR_PROTOCOL_NOT_FOUND;
267 }
268
269 int ffurl_open(URLContext **puc, const char *filename, int flags,
270                const AVIOInterruptCB *int_cb, AVDictionary **options)
271 {
272     int ret = ffurl_alloc(puc, filename, flags, int_cb);
273     if (ret)
274         return ret;
275     if (options && (*puc)->prot->priv_data_class &&
276         (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
277         goto fail;
278     ret = ffurl_connect(*puc, options);
279     if (!ret)
280         return 0;
281 fail:
282     ffurl_close(*puc);
283     *puc = NULL;
284     return ret;
285 }
286
287 static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
288                                          int size, int size_min,
289                                          int (*transfer_func)(URLContext *h,
290                                                               uint8_t *buf,
291                                                               int size))
292 {
293     int ret, len;
294     int fast_retries = 5;
295     int64_t wait_since = 0;
296
297     len = 0;
298     while (len < size_min) {
299         if (ff_check_interrupt(&h->interrupt_callback))
300             return AVERROR_EXIT;
301         ret = transfer_func(h, buf + len, size - len);
302         if (ret == AVERROR(EINTR))
303             continue;
304         if (h->flags & AVIO_FLAG_NONBLOCK)
305             return ret;
306         if (ret == AVERROR(EAGAIN)) {
307             ret = 0;
308             if (fast_retries) {
309                 fast_retries--;
310             } else {
311                 if (h->rw_timeout) {
312                     if (!wait_since)
313                         wait_since = av_gettime();
314                     else if (av_gettime() > wait_since + h->rw_timeout)
315                         return AVERROR(EIO);
316                 }
317                 av_usleep(1000);
318             }
319         } else if (ret < 1)
320             return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
321         if (ret)
322             fast_retries = FFMAX(fast_retries, 2);
323         len += ret;
324     }
325     return len;
326 }
327
328 int ffurl_read(URLContext *h, unsigned char *buf, int size)
329 {
330     if (!(h->flags & AVIO_FLAG_READ))
331         return AVERROR(EIO);
332     return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
333 }
334
335 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
336 {
337     if (!(h->flags & AVIO_FLAG_READ))
338         return AVERROR(EIO);
339     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
340 }
341
342 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
343 {
344     if (!(h->flags & AVIO_FLAG_WRITE))
345         return AVERROR(EIO);
346     /* avoid sending too big packets */
347     if (h->max_packet_size && size > h->max_packet_size)
348         return AVERROR(EIO);
349
350     return retry_transfer_wrapper(h, (unsigned char *)buf, size, size, (void*)h->prot->url_write);
351 }
352
353 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
354 {
355     int64_t ret;
356
357     if (!h->prot->url_seek)
358         return AVERROR(ENOSYS);
359     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
360     return ret;
361 }
362
363 int ffurl_closep(URLContext **hh)
364 {
365     URLContext *h= *hh;
366     int ret = 0;
367     if (!h)
368         return 0;     /* can happen when ffurl_open fails */
369
370     if (h->is_connected && h->prot->url_close)
371         ret = h->prot->url_close(h);
372 #if CONFIG_NETWORK
373     if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
374         ff_network_close();
375 #endif
376     if (h->prot->priv_data_size) {
377         if (h->prot->priv_data_class)
378             av_opt_free(h->priv_data);
379         av_freep(&h->priv_data);
380     }
381     av_freep(hh);
382     return ret;
383 }
384
385 int ffurl_close(URLContext *h)
386 {
387     return ffurl_closep(&h);
388 }
389
390
391 const char *avio_find_protocol_name(const char *url)
392 {
393     URLProtocol *p = url_find_protocol(url);
394
395     return p ? p->name : NULL;
396 }
397
398 int avio_check(const char *url, int flags)
399 {
400     URLContext *h;
401     int ret = ffurl_alloc(&h, url, flags, NULL);
402     if (ret)
403         return ret;
404
405     if (h->prot->url_check) {
406         ret = h->prot->url_check(h, flags);
407     } else {
408         ret = ffurl_connect(h, NULL);
409         if (ret >= 0)
410             ret = flags;
411     }
412
413     ffurl_close(h);
414     return ret;
415 }
416
417 int64_t ffurl_size(URLContext *h)
418 {
419     int64_t pos, size;
420
421     size = ffurl_seek(h, 0, AVSEEK_SIZE);
422     if (size < 0) {
423         pos = ffurl_seek(h, 0, SEEK_CUR);
424         if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
425             return size;
426         size++;
427         ffurl_seek(h, pos, SEEK_SET);
428     }
429     return size;
430 }
431
432 int ffurl_get_file_handle(URLContext *h)
433 {
434     if (!h->prot->url_get_file_handle)
435         return -1;
436     return h->prot->url_get_file_handle(h);
437 }
438
439 int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
440 {
441     if (!h->prot->url_get_multi_file_handle) {
442         if (!h->prot->url_get_file_handle)
443             return AVERROR(ENOSYS);
444         *handles = av_malloc(sizeof(**handles));
445         if (!*handles)
446             return AVERROR(ENOMEM);
447         *numhandles = 1;
448         *handles[0] = h->prot->url_get_file_handle(h);
449         return 0;
450     }
451     return h->prot->url_get_multi_file_handle(h, handles, numhandles);
452 }
453
454 int ffurl_shutdown(URLContext *h, int flags)
455 {
456     if (!h->prot->url_shutdown)
457         return AVERROR(EINVAL);
458     return h->prot->url_shutdown(h, flags);
459 }
460
461 int ff_check_interrupt(AVIOInterruptCB *cb)
462 {
463     int ret;
464     if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
465         return ret;
466     return 0;
467 }