]> 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 <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 #if FF_API_OLD_INTERRUPT_CB
88 static int default_interrupt_cb(void);
89 int (*url_interrupt_cb)(void) = default_interrupt_cb;
90 #endif
91
92 URLProtocol *av_protocol_next(URLProtocol *p)
93 {
94     return ffurl_protocol_next(p);
95 }
96
97 const char *avio_enum_protocols(void **opaque, int output)
98 {
99     URLProtocol **p = opaque;
100     *p = ffurl_protocol_next(*p);
101     if (!*p) return NULL;
102     if ((output && (*p)->url_write) || (!output && (*p)->url_read))
103         return (*p)->name;
104     return avio_enum_protocols(opaque, output);
105 }
106
107 int ffurl_register_protocol(URLProtocol *protocol, int size)
108 {
109     URLProtocol **p;
110     if (size < sizeof(URLProtocol)) {
111         URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
112         memcpy(temp, protocol, size);
113         protocol = temp;
114     }
115     p = &first_protocol;
116     while (*p != NULL) p = &(*p)->next;
117     *p = protocol;
118     protocol->next = NULL;
119     return 0;
120 }
121
122 static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
123                                    const char *filename, int flags,
124                                    const AVIOInterruptCB *int_cb)
125 {
126     URLContext *uc;
127     int err;
128
129 #if CONFIG_NETWORK
130     if (!ff_network_init())
131         return AVERROR(EIO);
132 #endif
133     uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
134     if (!uc) {
135         err = AVERROR(ENOMEM);
136         goto fail;
137     }
138     uc->av_class = &ffurl_context_class;
139     uc->filename = (char *) &uc[1];
140     strcpy(uc->filename, filename);
141     uc->prot = up;
142     uc->flags = flags;
143     uc->is_streamed = 0; /* default = not streamed */
144     uc->max_packet_size = 0; /* default: stream file */
145     if (up->priv_data_size) {
146         uc->priv_data = av_mallocz(up->priv_data_size);
147         if (up->priv_data_class) {
148             char *start = strchr(uc->filename, ',');
149             *(const AVClass**)uc->priv_data = up->priv_data_class;
150             av_opt_set_defaults(uc->priv_data);
151             if(start){
152                 int ret= 0;
153                 char *p= start;
154                 char sep= *++p;
155                 char *key, *val;
156                 p++;
157                 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
158                     *val= *key= 0;
159                     ret= av_opt_set(uc->priv_data, p, key+1, 0);
160                     if (ret == AVERROR_OPTION_NOT_FOUND)
161                         av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
162                     *val= *key= sep;
163                     p= val+1;
164                 }
165                 if(ret<0 || p!=key){
166                     av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
167                     av_freep(&uc->priv_data);
168                     av_freep(&uc);
169                     goto fail;
170                 }
171                 memmove(start, key+1, strlen(key));
172             }
173         }
174     }
175     if (int_cb)
176         uc->interrupt_callback = *int_cb;
177
178     *puc = uc;
179     return 0;
180  fail:
181     *puc = NULL;
182 #if CONFIG_NETWORK
183     ff_network_close();
184 #endif
185     return err;
186 }
187
188 int ffurl_connect(URLContext* uc, AVDictionary **options)
189 {
190     int err =
191 #if !FF_API_OLD_AVIO
192         uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
193 #endif
194         uc->prot->url_open(uc, uc->filename, uc->flags);
195     if (err)
196         return err;
197     uc->is_connected = 1;
198     //We must be careful here as ffurl_seek() could be slow, for example for http
199     if(   (uc->flags & AVIO_FLAG_WRITE)
200        || !strcmp(uc->prot->name, "file"))
201         if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
202             uc->is_streamed= 1;
203     return 0;
204 }
205
206 #if FF_API_OLD_AVIO
207 int url_open_protocol (URLContext **puc, struct URLProtocol *up,
208                        const char *filename, int flags)
209 {
210     int ret;
211
212     ret = url_alloc_for_protocol(puc, up, filename, flags, NULL);
213     if (ret)
214         goto fail;
215     ret = ffurl_connect(*puc, NULL);
216     if (!ret)
217         return 0;
218  fail:
219     ffurl_close(*puc);
220     *puc = NULL;
221     return ret;
222 }
223 int url_alloc(URLContext **puc, const char *filename, int flags)
224 {
225     return ffurl_alloc(puc, filename, flags, NULL);
226 }
227 int url_connect(URLContext* uc)
228 {
229     return ffurl_connect(uc, NULL);
230 }
231 int url_open(URLContext **puc, const char *filename, int flags)
232 {
233     return ffurl_open(puc, filename, flags, NULL, NULL);
234 }
235 int url_read(URLContext *h, unsigned char *buf, int size)
236 {
237     return ffurl_read(h, buf, size);
238 }
239 int url_read_complete(URLContext *h, unsigned char *buf, int size)
240 {
241     return ffurl_read_complete(h, buf, size);
242 }
243 int url_write(URLContext *h, const unsigned char *buf, int size)
244 {
245     return ffurl_write(h, buf, size);
246 }
247 int64_t url_seek(URLContext *h, int64_t pos, int whence)
248 {
249     return ffurl_seek(h, pos, whence);
250 }
251 int url_close(URLContext *h)
252 {
253     return ffurl_close(h);
254 }
255 int64_t url_filesize(URLContext *h)
256 {
257     return ffurl_size(h);
258 }
259 int url_get_file_handle(URLContext *h)
260 {
261     return ffurl_get_file_handle(h);
262 }
263 int url_get_max_packet_size(URLContext *h)
264 {
265     return h->max_packet_size;
266 }
267 void url_get_filename(URLContext *h, char *buf, int buf_size)
268 {
269     av_strlcpy(buf, h->filename, buf_size);
270 }
271 void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
272 {
273     avio_set_interrupt_cb(interrupt_cb);
274 }
275 int av_register_protocol2(URLProtocol *protocol, int size)
276 {
277     return ffurl_register_protocol(protocol, size);
278 }
279 #endif
280
281 #define URL_SCHEME_CHARS                        \
282     "abcdefghijklmnopqrstuvwxyz"                \
283     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                \
284     "0123456789+-."
285
286 int ffurl_alloc(URLContext **puc, const char *filename, int flags,
287                 const AVIOInterruptCB *int_cb)
288 {
289     URLProtocol *up = NULL;
290     char proto_str[128], proto_nested[128], *ptr;
291     size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
292
293     if (!first_protocol) {
294         av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
295                                      "Missing call to av_register_all()?\n");
296     }
297
298     if (filename[proto_len] != ':' &&  filename[proto_len] != ',' || is_dos_path(filename))
299         strcpy(proto_str, "file");
300     else
301         av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
302
303     if ((ptr = strchr(proto_str, ',')))
304         *ptr = '\0';
305     av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
306     if ((ptr = strchr(proto_nested, '+')))
307         *ptr = '\0';
308
309     while (up = ffurl_protocol_next(up)) {
310         if (!strcmp(proto_str, up->name))
311             return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
312         if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
313             !strcmp(proto_nested, up->name))
314             return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
315     }
316     *puc = NULL;
317     return AVERROR(ENOENT);
318 }
319
320 int ffurl_open(URLContext **puc, const char *filename, int flags,
321                const AVIOInterruptCB *int_cb, AVDictionary **options)
322 {
323     int ret = ffurl_alloc(puc, filename, flags, int_cb);
324     if (ret)
325         return ret;
326     if (options && (*puc)->prot->priv_data_class &&
327         (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
328         goto fail;
329     ret = ffurl_connect(*puc, options);
330     if (!ret)
331         return 0;
332 fail:
333     ffurl_close(*puc);
334     *puc = NULL;
335     return ret;
336 }
337
338 static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
339                                          int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
340 {
341     int ret, len;
342     int fast_retries = 5;
343
344     len = 0;
345     while (len < size_min) {
346         ret = transfer_func(h, buf+len, size-len);
347         if (ret == AVERROR(EINTR))
348             continue;
349         if (h->flags & AVIO_FLAG_NONBLOCK)
350             return ret;
351         if (ret == AVERROR(EAGAIN)) {
352             ret = 0;
353             if (fast_retries)
354                 fast_retries--;
355             else
356                 usleep(1000);
357         } else if (ret < 1)
358             return ret < 0 ? ret : len;
359         if (ret)
360            fast_retries = FFMAX(fast_retries, 2);
361         len += ret;
362         if (len < size && ff_check_interrupt(&h->interrupt_callback))
363             return AVERROR_EXIT;
364     }
365     return len;
366 }
367
368 int ffurl_read(URLContext *h, unsigned char *buf, int size)
369 {
370     if (!(h->flags & AVIO_FLAG_READ))
371         return AVERROR(EIO);
372     return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
373 }
374
375 int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
376 {
377     if (!(h->flags & AVIO_FLAG_READ))
378         return AVERROR(EIO);
379     return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
380 }
381
382 int ffurl_write(URLContext *h, const unsigned char *buf, int size)
383 {
384     if (!(h->flags & AVIO_FLAG_WRITE))
385         return AVERROR(EIO);
386     /* avoid sending too big packets */
387     if (h->max_packet_size && size > h->max_packet_size)
388         return AVERROR(EIO);
389
390     return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
391 }
392
393 int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
394 {
395     int64_t ret;
396
397     if (!h->prot->url_seek)
398         return AVERROR(ENOSYS);
399     ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
400     return ret;
401 }
402
403 int ffurl_close(URLContext *h)
404 {
405     int ret = 0;
406     if (!h) return 0; /* can happen when ffurl_open fails */
407
408     if (h->is_connected && h->prot->url_close)
409         ret = h->prot->url_close(h);
410 #if CONFIG_NETWORK
411     ff_network_close();
412 #endif
413     if (h->prot->priv_data_size) {
414         if (h->prot->priv_data_class)
415             av_opt_free(h->priv_data);
416         av_free(h->priv_data);
417     }
418     av_free(h);
419     return ret;
420 }
421
422 #if FF_API_OLD_AVIO
423 int url_exist(const char *filename)
424 {
425     URLContext *h;
426     if (ffurl_open(&h, filename, AVIO_FLAG_READ, NULL, NULL) < 0)
427         return 0;
428     ffurl_close(h);
429     return 1;
430 }
431 #endif
432
433 int avio_check(const char *url, int flags)
434 {
435     URLContext *h;
436     int ret = ffurl_alloc(&h, url, flags, NULL);
437     if (ret)
438         return ret;
439
440     if (h->prot->url_check) {
441         ret = h->prot->url_check(h, flags);
442     } else {
443         ret = ffurl_connect(h, NULL);
444         if (ret >= 0)
445             ret = flags;
446     }
447
448     ffurl_close(h);
449     return ret;
450 }
451
452 int64_t ffurl_size(URLContext *h)
453 {
454     int64_t pos, size;
455
456     size= ffurl_seek(h, 0, AVSEEK_SIZE);
457     if(size<0){
458         pos = ffurl_seek(h, 0, SEEK_CUR);
459         if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
460             return size;
461         size++;
462         ffurl_seek(h, pos, SEEK_SET);
463     }
464     return size;
465 }
466
467 int ffurl_get_file_handle(URLContext *h)
468 {
469     if (!h->prot->url_get_file_handle)
470         return -1;
471     return h->prot->url_get_file_handle(h);
472 }
473
474 #if FF_API_OLD_INTERRUPT_CB
475 static int default_interrupt_cb(void)
476 {
477     return 0;
478 }
479
480 void avio_set_interrupt_cb(int (*interrupt_cb)(void))
481 {
482     if (!interrupt_cb)
483         interrupt_cb = default_interrupt_cb;
484     url_interrupt_cb = interrupt_cb;
485 }
486 #endif
487
488 int ff_check_interrupt(AVIOInterruptCB *cb)
489 {
490     int ret;
491     if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
492         return ret;
493 #if FF_API_OLD_INTERRUPT_CB
494     return url_interrupt_cb();
495 #else
496     return 0;
497 #endif
498 }
499
500 #if FF_API_OLD_AVIO
501 int av_url_read_pause(URLContext *h, int pause)
502 {
503     if (!h->prot->url_read_pause)
504         return AVERROR(ENOSYS);
505     return h->prot->url_read_pause(h, pause);
506 }
507
508 int64_t av_url_read_seek(URLContext *h,
509         int stream_index, int64_t timestamp, int flags)
510 {
511     if (!h->prot->url_read_seek)
512         return AVERROR(ENOSYS);
513     return h->prot->url_read_seek(h, stream_index, timestamp, flags);
514 }
515 #endif