FFmpeg
Loading...
Searching...
No Matches
avio.c
Go to the documentation of this file.
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 "config_components.h"
23
24#include <stdlib.h>
25
26#include "libavutil/avstring.h"
27#include "libavutil/dict.h"
28#include "libavutil/mem.h"
29#include "libavutil/opt.h"
30#include "libavutil/time.h"
31#include "libavutil/avassert.h"
32#include "avio_internal.h"
33#include "os_support.h"
34#include "internal.h"
35#if CONFIG_NETWORK
36#include "network.h"
37#endif
38#include "url.h"
39
40#define IO_BUFFER_SIZE 32768
41#define NETWORK_IO_BUFFER_SIZE 262144
42
43/** @name Logging context. */
44/*@{*/
45static const char *urlcontext_to_name(void *ptr)
46{
47 URLContext *h = (URLContext *)ptr;
48 if (h->prot)
49 return h->prot->name;
50 else
51 return "NULL";
52}
53
54static void *urlcontext_child_next(void *obj, void *prev)
55{
56 URLContext *h = obj;
57 if (!prev && h->priv_data && h->prot->priv_data_class)
58 return h->priv_data;
59 return NULL;
60}
61
62#define OFFSET(x) offsetof(URLContext,x)
63#define E AV_OPT_FLAG_ENCODING_PARAM
64#define D AV_OPT_FLAG_DECODING_PARAM
65static const AVOption urlcontext_options[] = {
66 {"protocol_whitelist", "List of protocols that are allowed to be used", OFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
67 {"protocol_blacklist", "List of protocols that are not allowed to be used", OFFSET(protocol_blacklist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
68 {"rw_timeout", "Timeout for IO operations (in microseconds)", offsetof(URLContext, rw_timeout), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_DECODING_PARAM },
69 {"prefer_libcurl", "use the libcurl protocol for http(s) URLs when available", OFFSET(prefer_libcurl), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, D },
70 { NULL }
71};
72
73static const AVClass url_context_class = {
74 .class_name = "URLContext",
75 .item_name = urlcontext_to_name,
76 .option = urlcontext_options,
77 .version = LIBAVUTIL_VERSION_INT,
78 .child_next = urlcontext_child_next,
79 .child_class_iterate = ff_urlcontext_child_class_iterate,
80};
81/*@}*/
82
83static void *avio_child_next(void *obj, void *prev)
84{
85 AVIOContext *s = obj;
86 return prev ? NULL : s->opaque;
87}
88
89static const AVClass *child_class_iterate(void **iter)
90{
91 const AVClass *c = *iter ? NULL : &url_context_class;
92 *iter = (void*)(uintptr_t)c;
93 return c;
94}
95
96#define AVIOOFFSET(x) offsetof(AVIOContext,x)
97#define E AV_OPT_FLAG_ENCODING_PARAM
98#define D AV_OPT_FLAG_DECODING_PARAM
99static const AVOption avio_options[] = {
100 {"protocol_whitelist", "List of protocols that are allowed to be used", AVIOOFFSET(protocol_whitelist), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D },
101 { NULL },
102};
103
105 .class_name = "AVIOContext",
106 .item_name = av_default_item_name,
107 .version = LIBAVUTIL_VERSION_INT,
108 .option = avio_options,
109 .child_next = avio_child_next,
110 .child_class_iterate = child_class_iterate,
111};
112
114{
115 if (!s)
116 return NULL;
117
118 if (s->opaque && s->read_packet == ffurl_read2)
119 return s->opaque;
120 else
121 return NULL;
122}
123
125 const char *filename, int flags,
126 const AVIOInterruptCB *int_cb)
127{
128 URLContext *uc;
129 int err;
130
131#if CONFIG_NETWORK
132 if (up->flags & URL_PROTOCOL_FLAG_NETWORK && (err = ff_network_init()) < 0)
133 return err;
134#endif
135 if ((flags & AVIO_FLAG_READ) && !up->url_read) {
137 "Impossible to open the '%s' protocol for reading\n", up->name);
138 return AVERROR(EIO);
139 }
140 if ((flags & AVIO_FLAG_WRITE) && !up->url_write) {
142 "Impossible to open the '%s' protocol for writing\n", up->name);
143 return AVERROR(EIO);
144 }
145 uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
146 if (!uc) {
147 err = AVERROR(ENOMEM);
148 goto fail;
149 }
151 uc->filename = (char *)&uc[1];
152 strcpy(uc->filename, filename);
153 uc->prot = up;
154 uc->flags = flags;
155 uc->is_streamed = 0; /* default = not streamed */
157 uc->max_packet_size = 0; /* default: stream file */
158 if (up->priv_data_size) {
160 if (!uc->priv_data) {
161 err = AVERROR(ENOMEM);
162 goto fail;
163 }
164 if (up->priv_data_class) {
165 char *start;
166 *(const AVClass **)uc->priv_data = up->priv_data_class;
168 if (av_strstart(uc->filename, up->name, (const char**)&start) && *start == ',') {
169 int ret= 0;
170 char *p= start;
171 char sep= *++p;
172 char *key, *val;
173 p++;
174
175 if (strcmp(up->name, "subfile"))
176 ret = AVERROR(EINVAL);
177
178 while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
179 *val= *key= 0;
180 ret = av_opt_set(uc->priv_data, p, key+1, 0);
181 if (ret == AVERROR_OPTION_NOT_FOUND)
182 av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
183 *val= *key= sep;
184 p= val+1;
185 }
186 if(ret<0 || p!=key){
187 av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
188 err = AVERROR(EINVAL);
189 goto fail;
190 }
191 memmove(start, key+1, strlen(key));
192 }
193 }
194 }
195 if (int_cb)
197
198 *puc = uc;
199 return 0;
200fail:
201 *puc = NULL;
202 if (uc)
203 av_freep(&uc->priv_data);
204 av_freep(&uc);
205#if CONFIG_NETWORK
208#endif
209 return err;
210}
211
213{
214 int err;
215 AVDictionary *tmp_opts = NULL;
217
218 if (!options)
219 options = &tmp_opts;
220
221 // Check that URLContext was initialized correctly and lists are matching if set
222 av_assert0(!(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
223 (uc->protocol_whitelist && !strcmp(uc->protocol_whitelist, e->value)));
224 av_assert0(!(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
225 (uc->protocol_blacklist && !strcmp(uc->protocol_blacklist, e->value)));
226
227 if (uc->protocol_whitelist && av_match_list(uc->prot->name, uc->protocol_whitelist, ',') <= 0) {
228 av_log(uc, AV_LOG_ERROR, "Protocol '%s' not on whitelist '%s'!\n", uc->prot->name, uc->protocol_whitelist);
229 return AVERROR(EINVAL);
230 }
231
232 if (uc->protocol_blacklist && av_match_list(uc->prot->name, uc->protocol_blacklist, ',') > 0) {
233 av_log(uc, AV_LOG_ERROR, "Protocol '%s' on blacklist '%s'!\n", uc->prot->name, uc->protocol_blacklist);
234 return AVERROR(EINVAL);
235 }
236
237 if (!uc->protocol_whitelist && uc->prot->default_whitelist) {
238 av_log(uc, AV_LOG_DEBUG, "Setting default whitelist '%s'\n", uc->prot->default_whitelist);
240 if (!uc->protocol_whitelist) {
241 return AVERROR(ENOMEM);
242 }
243 } else if (!uc->protocol_whitelist)
244 av_log(uc, AV_LOG_DEBUG, "No default whitelist set\n"); // This should be an error once all declare a default whitelist
245
246 if ((err = av_dict_set(options, "protocol_whitelist", uc->protocol_whitelist, 0)) < 0)
247 return err;
248 if ((err = av_dict_set(options, "protocol_blacklist", uc->protocol_blacklist, 0)) < 0)
249 goto fail;
250
251 err =
252 uc->prot->url_open2 ? uc->prot->url_open2(uc,
253 uc->filename,
254 uc->flags,
255 options) :
256 uc->prot->url_open(uc, uc->filename, uc->flags);
257
258 av_dict_set(options, "protocol_whitelist", NULL, 0);
259 av_dict_set(options, "protocol_blacklist", NULL, 0);
260
261 if (err)
262 return err;
263 uc->is_connected = 1;
264 /* We must be careful here as ffurl_seek() could be slow,
265 * for example for http */
266 if ((uc->flags & AVIO_FLAG_WRITE) || !strcmp(uc->prot->name, "file"))
267 if (!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
268 uc->is_streamed = 1;
269 return 0;
270
271fail:
272 if (options == &tmp_opts)
273 av_dict_free(&tmp_opts);
274 return err;
275}
276
278{
279 av_assert0(!*c);
280 if (s->prot->url_accept)
281 return s->prot->url_accept(s, c);
282 return AVERROR(EBADF);
283}
284
286{
287 int ret;
288 URLContext *sc = s->opaque;
289 URLContext *cc = NULL;
290 ret = ffurl_accept(sc, &cc);
291 if (ret < 0)
292 return ret;
293 return ffio_fdopen(c, cc);
294}
295
297{
298 int ret;
299 if (c->prot->url_handshake) {
300 ret = c->prot->url_handshake(c);
301 if (ret)
302 return ret;
303 }
304 c->is_connected = 1;
305 return 0;
306}
307
309{
310 URLContext *cc = c->opaque;
311 return ffurl_handshake(cc);
312}
313
314#define URL_SCHEME_CHARS \
315 "abcdefghijklmnopqrstuvwxyz" \
316 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
317 "0123456789+-."
318
319static const struct URLProtocol *url_find_protocol(const char *filename)
320{
321 const URLProtocol **protocols;
322 char proto_str[128], proto_nested[128], *ptr;
323 size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
324 int i;
325
326 if (filename[proto_len] != ':' &&
327 (strncmp(filename, "subfile,", 8) || !strchr(filename + proto_len + 1, ':')) ||
328 is_dos_path(filename))
329 strcpy(proto_str, "file");
330 else
331 av_strlcpy(proto_str, filename,
332 FFMIN(proto_len + 1, sizeof(proto_str)));
333
334 av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
335 if ((ptr = strchr(proto_nested, '+')))
336 *ptr = '\0';
337
338 protocols = ffurl_get_protocols(NULL, NULL);
339 if (!protocols)
340 return NULL;
341 for (i = 0; protocols[i]; i++) {
342 const URLProtocol *up = protocols[i];
343 if (!strcmp(proto_str, up->name)) {
344 av_freep(&protocols);
345 return up;
346 }
348 !strcmp(proto_nested, up->name)) {
349 av_freep(&protocols);
350 return up;
351 }
352 }
353 av_freep(&protocols);
354 if (av_strstart(filename, "https:", NULL) || av_strstart(filename, "tls:", NULL) ||
355 av_strstart(filename, "dtls:", NULL))
356 av_log(NULL, AV_LOG_WARNING, "https or dtls protocol not found, recompile FFmpeg with "
357 "openssl, gnutls or securetransport enabled.\n");
358
359 return NULL;
360}
361
362int ffurl_alloc(URLContext **puc, const char *filename, int flags,
363 const AVIOInterruptCB *int_cb)
364{
365 const URLProtocol *p = NULL;
366
367 p = url_find_protocol(filename);
368 if (p)
369 return url_alloc_for_protocol(puc, p, filename, flags, int_cb);
370
371 *puc = NULL;
373}
374
375#if CONFIG_LIBCURL_PROTOCOL
377
378/* Decide whether an http(s) URL should be routed to the libcurl protocol instead
379 * of the native one. Controlled by the per-open "prefer_libcurl" option. */
380static int prefer_libcurl(const char *filename, AVDictionary **options,
381 URLContext *parent)
382{
383 URLContext dummy = { .av_class = &url_context_class };
385
386 if (!av_strstart(filename, "http://", NULL) &&
387 !av_strstart(filename, "https://", NULL))
388 return 0;
389
390 if (parent && parent->prefer_libcurl)
391 return 1;
392
393 if (!options || !(e = av_dict_get(*options, "prefer_libcurl", NULL, 0)))
394 return 0;
395 if (av_opt_set(&dummy, "prefer_libcurl", e->value, 0) < 0)
396 return 0;
397
398 return dummy.prefer_libcurl;
399}
400#endif
401
402static int url_open_whitelist(URLContext **puc, const char *filename, int flags,
404 const char *whitelist, const char* blacklist,
405 URLContext *parent, AVFormatContext *avfc)
406{
407 AVDictionary *tmp_opts = NULL;
409 int ret;
410
411#if CONFIG_LIBCURL_PROTOCOL
412 /* The option itself is applied to the URLContext further down; here it only
413 * picks the protocol, before the context exists. */
414 if (prefer_libcurl(filename, options, parent))
415 ret = url_alloc_for_protocol(puc, &ff_libcurl_protocol, filename, flags,
416 int_cb);
417 else
418#endif
419 ret = ffurl_alloc(puc, filename, flags, int_cb);
420 if (ret < 0)
421 return ret;
422 (*puc)->avfc = avfc;
423 if (parent) {
424 ret = av_opt_copy(*puc, parent);
425 if (ret < 0)
426 goto fail;
427 }
428 if (options &&
429 (ret = av_opt_set_dict(*puc, options)) < 0)
430 goto fail;
431 if (options && (*puc)->prot->priv_data_class &&
432 (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
433 goto fail;
434
435 if (!options)
436 options = &tmp_opts;
437
438 av_assert0(!whitelist ||
439 !(e=av_dict_get(*options, "protocol_whitelist", NULL, 0)) ||
440 !strcmp(whitelist, e->value));
441 av_assert0(!blacklist ||
442 !(e=av_dict_get(*options, "protocol_blacklist", NULL, 0)) ||
443 !strcmp(blacklist, e->value));
444
445 if ((ret = av_dict_set(options, "protocol_whitelist", whitelist, 0)) < 0)
446 goto fail;
447
448 if ((ret = av_dict_set(options, "protocol_blacklist", blacklist, 0)) < 0)
449 goto fail;
450
451 if ((ret = av_opt_set_dict(*puc, options)) < 0)
452 goto fail;
453
454 ret = ffurl_connect(*puc, options);
455
456 if (!ret) {
457 if (parent)
458 parent->uses_network |= (*puc)->uses_network;
459 return 0;
460 }
461fail:
462 ffurl_closep(puc);
463 return ret;
464}
465
466int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags,
468 const char *whitelist, const char* blacklist,
469 URLContext *parent)
470{
471 return url_open_whitelist(puc, filename, flags, int_cb, options,
472 whitelist, blacklist, parent, NULL);
473}
474
476{
477 AVIOContext *s;
478 uint8_t *buffer = NULL;
479 int buffer_size, max_packet_size;
480
481 max_packet_size = h->max_packet_size;
482 if (max_packet_size) {
483 buffer_size = max_packet_size; /* no need to bufferize more than one packet */
484 } else if (h->uses_network && !(h->flags & AVIO_FLAG_WRITE)) {
485 buffer_size = NETWORK_IO_BUFFER_SIZE;
486 } else {
487 buffer_size = IO_BUFFER_SIZE;
488 }
489 if (!(h->flags & AVIO_FLAG_WRITE) && h->is_streamed) {
490 if (buffer_size > INT_MAX/2)
491 return AVERROR(EINVAL);
492 buffer_size *= 2;
493 }
494 buffer = av_malloc(buffer_size);
495 if (!buffer)
496 return AVERROR(ENOMEM);
497
498 *sp = avio_alloc_context(buffer, buffer_size, h->flags & AVIO_FLAG_WRITE, h,
500 if (!*sp) {
502 return AVERROR(ENOMEM);
503 }
504 s = *sp;
505 if (h->protocol_whitelist) {
506 s->protocol_whitelist = av_strdup(h->protocol_whitelist);
507 if (!s->protocol_whitelist) {
508 avio_closep(sp);
509 return AVERROR(ENOMEM);
510 }
511 }
512 if (h->protocol_blacklist) {
513 s->protocol_blacklist = av_strdup(h->protocol_blacklist);
514 if (!s->protocol_blacklist) {
515 avio_closep(sp);
516 return AVERROR(ENOMEM);
517 }
518 }
519 s->direct = h->flags & AVIO_FLAG_DIRECT;
520
521 s->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
522 s->max_packet_size = max_packet_size;
523 s->min_packet_size = h->min_packet_size;
524 if(h->prot) {
525 s->read_pause = h->prot->url_read_pause;
526 s->read_seek = h->prot->url_read_seek;
527
528 if (h->prot->url_read_seek)
529 s->seekable |= AVIO_SEEKABLE_TIME;
530 }
531 ((FFIOContext*)s)->short_seek_get = ffurl_get_short_seek;
532 s->av_class = &ff_avio_class;
533 return 0;
534}
535
536int ffio_open_whitelist2(AVIOContext **s, const char *filename, int flags,
538 const char *whitelist, const char *blacklist,
539 AVFormatContext *avfc)
540{
541 URLContext *h;
542 int err;
543
544 *s = NULL;
545
546 err = url_open_whitelist(&h, filename, flags, int_cb, options, whitelist,
547 blacklist, NULL, avfc);
548 if (err < 0)
549 return err;
550 err = ffio_fdopen(s, h);
551 if (err < 0) {
552 ffurl_close(h);
553 return err;
554 }
555 return 0;
556}
557
558int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags,
560 const char *whitelist, const char *blacklist)
561{
562 return ffio_open_whitelist2(s, filename, flags, int_cb, options,
563 whitelist, blacklist, NULL);
564}
565
566int avio_open2(AVIOContext **s, const char *filename, int flags,
568{
569 return ffio_open_whitelist(s, filename, flags, int_cb, options, NULL, NULL);
570}
571
572int avio_open(AVIOContext **s, const char *filename, int flags)
573{
574 return avio_open2(s, filename, flags, NULL, NULL);
575}
576
577
578static inline int retry_transfer_wrapper(URLContext *h, uint8_t *buf,
579 const uint8_t *cbuf,
580 int size, int size_min,
581 int read)
582{
583 int ret, len;
584 int fast_retries = 5;
585 int64_t wait_since = 0;
586
587 len = 0;
588 while (len < size_min) {
589 if (ff_check_interrupt(&h->interrupt_callback))
590 return AVERROR_EXIT;
591 ret = read ? h->prot->url_read (h, buf + len, size - len):
592 h->prot->url_write(h, cbuf + len, size - len);
593 if (ret == AVERROR(EINTR))
594 continue;
595 if (h->flags & AVIO_FLAG_NONBLOCK)
596 return ret;
597 if (ret == AVERROR(EAGAIN)) {
598 ret = 0;
599 if (fast_retries) {
600 fast_retries--;
601 } else {
602 if (h->rw_timeout) {
603 if (!wait_since)
604 wait_since = av_gettime_relative();
605 else if (av_gettime_relative() > wait_since + h->rw_timeout)
606 return AVERROR(EIO);
607 }
608 av_usleep(1000);
609 }
610 } else if (ret == AVERROR_EOF)
611 return (len > 0) ? len : AVERROR_EOF;
612 else if (ret < 0)
613 return ret;
614 if (ret) {
615 fast_retries = FFMAX(fast_retries, 2);
616 wait_since = 0;
617 }
618 len += ret;
619 }
620 return len;
621}
622
623int ffurl_read2(void *urlcontext, uint8_t *buf, int size)
624{
625 URLContext *h = urlcontext;
626
627 if (!(h->flags & AVIO_FLAG_READ))
628 return AVERROR(EIO);
629 return retry_transfer_wrapper(h, buf, NULL, size, 1, 1);
630}
631
632int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
633{
634 if (!(h->flags & AVIO_FLAG_READ))
635 return AVERROR(EIO);
636 return retry_transfer_wrapper(h, buf, NULL, size, size, 1);
637}
638
639int ffurl_write2(void *urlcontext, const uint8_t *buf, int size)
640{
641 URLContext *h = urlcontext;
642
643 if (!(h->flags & AVIO_FLAG_WRITE))
644 return AVERROR(EIO);
645 /* avoid sending too big packets */
646 if (h->max_packet_size && size > h->max_packet_size)
647 return AVERROR(EIO);
648
649 return retry_transfer_wrapper(h, NULL, buf, size, size, 0);
650}
651
652int64_t ffurl_seek2(void *urlcontext, int64_t pos, int whence)
653{
654 URLContext *h = urlcontext;
655 int64_t ret;
656
657 if (!h->prot->url_seek)
658 return AVERROR(ENOSYS);
659 ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
660 return ret;
661}
662
664{
665 URLContext *h= *hh;
666 int ret = 0;
667 if (!h)
668 return 0; /* can happen when ffurl_open fails */
669
670 if (h->is_connected && h->prot->url_close)
671 ret = h->prot->url_close(h);
672#if CONFIG_NETWORK
673 if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
675#endif
676 if (h->prot->priv_data_size) {
677 if (h->prot->priv_data_class)
678 av_opt_free(h->priv_data);
679 av_freep(&h->priv_data);
680 }
681 av_opt_free(h);
682 av_freep(hh);
683 return ret;
684}
685
687{
688 return ffurl_closep(&h);
689}
690
692{
693 FFIOContext *const ctx = ffiocontext(s);
694 URLContext *h;
695 int ret, error;
696
697 if (!s)
698 return 0;
699
700 avio_flush(s);
701 h = s->opaque;
702 s->opaque = NULL;
703
704 av_freep(&s->buffer);
705 if (s->write_flag)
707 "Statistics: %"PRId64" bytes written, %d seeks, %d writeouts\n",
708 ctx->bytes_written, ctx->seek_count, ctx->writeout_count);
709 else
710 av_log(s, AV_LOG_VERBOSE, "Statistics: %"PRId64" bytes read, %d seeks\n",
711 ctx->bytes_read, ctx->seek_count);
712 av_opt_free(s);
713
714 error = s->error;
716
717 ret = ffurl_close(h);
718 if (ret < 0)
719 return ret;
720
721 return error;
722}
723
725{
726 int ret = avio_close(*s);
727 *s = NULL;
728 return ret;
729}
730
731
732const char *avio_find_protocol_name(const char *url)
733{
734 const URLProtocol *p = url_find_protocol(url);
735
736 return p ? p->name : NULL;
737}
738
739int avio_check(const char *url, int flags)
740{
741 URLContext *h;
742 int ret = ffurl_alloc(&h, url, flags, NULL);
743 if (ret < 0)
744 return ret;
745
746 if (h->prot->url_check) {
747 ret = h->prot->url_check(h, flags);
748 } else {
749 ret = ffurl_connect(h, NULL);
750 if (ret >= 0)
751 ret = flags;
752 }
753
754 ffurl_close(h);
755 return ret;
756}
757
758int ffurl_move(const char *url_src, const char *url_dst)
759{
760 URLContext *h_src, *h_dst;
761 int ret = ffurl_alloc(&h_src, url_src, AVIO_FLAG_READ_WRITE, NULL);
762 if (ret < 0)
763 return ret;
764 ret = ffurl_alloc(&h_dst, url_dst, AVIO_FLAG_WRITE, NULL);
765 if (ret < 0) {
766 ffurl_close(h_src);
767 return ret;
768 }
769
770 if (h_src->prot == h_dst->prot && h_src->prot->url_move)
771 ret = h_src->prot->url_move(h_src, h_dst);
772 else
773 ret = AVERROR(ENOSYS);
774
775 ffurl_close(h_src);
776 ffurl_close(h_dst);
777 return ret;
778}
779
780int ffurl_delete(const char *url)
781{
782 URLContext *h;
783 int ret = ffurl_alloc(&h, url, AVIO_FLAG_WRITE, NULL);
784 if (ret < 0)
785 return ret;
786
787 if (h->prot->url_delete)
788 ret = h->prot->url_delete(h);
789 else
790 ret = AVERROR(ENOSYS);
791
792 ffurl_close(h);
793 return ret;
794}
795
799
801{
802 URLContext *h = NULL;
804 int ret;
805 av_assert0(s);
806
807 ctx = av_mallocz(sizeof(*ctx));
808 if (!ctx) {
809 ret = AVERROR(ENOMEM);
810 goto fail;
811 }
812
813 if ((ret = ffurl_alloc(&h, url, AVIO_FLAG_READ, NULL)) < 0)
814 goto fail;
815
816 if (h->prot->url_open_dir && h->prot->url_read_dir && h->prot->url_close_dir) {
817 if (options && h->prot->priv_data_class &&
818 (ret = av_opt_set_dict(h->priv_data, options)) < 0)
819 goto fail;
820 ret = h->prot->url_open_dir(h);
821 } else
822 ret = AVERROR(ENOSYS);
823 if (ret < 0)
824 goto fail;
825
826 h->is_connected = 1;
827 ctx->url_context = h;
828 *s = ctx;
829 return 0;
830
831 fail:
832 av_free(ctx);
833 *s = NULL;
834 ffurl_close(h);
835 return ret;
836}
837
839{
840 URLContext *h;
841 int ret;
842
843 if (!s || !s->url_context)
844 return AVERROR(EINVAL);
845 h = s->url_context;
846 if ((ret = h->prot->url_read_dir(h, next)) < 0)
848 return ret;
849}
850
852{
853 URLContext *h;
854
855 av_assert0(s);
856 if (!(*s) || !(*s)->url_context)
857 return AVERROR(EINVAL);
858 h = (*s)->url_context;
859 h->prot->url_close_dir(h);
860 ffurl_close(h);
861 av_freep(s);
862 *s = NULL;
863 return 0;
864}
865
867{
868 if (!entry || !*entry)
869 return;
870 av_free((*entry)->name);
872}
873
875{
877
879 if (size < 0) {
880 pos = ffurl_seek(h, 0, SEEK_CUR);
881 if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
882 return size;
883 size++;
884 ffurl_seek(h, pos, SEEK_SET);
885 }
886 return size;
887}
888
890{
891 if (!h || !h->prot || !h->prot->url_get_file_handle)
892 return -1;
893 return h->prot->url_get_file_handle(h);
894}
895
896int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
897{
898 if (!h || !h->prot)
899 return AVERROR(ENOSYS);
900 if (!h->prot->url_get_multi_file_handle) {
901 if (!h->prot->url_get_file_handle)
902 return AVERROR(ENOSYS);
903 *handles = av_malloc(sizeof(**handles));
904 if (!*handles)
905 return AVERROR(ENOMEM);
906 *numhandles = 1;
907 *handles[0] = h->prot->url_get_file_handle(h);
908 return 0;
909 }
910 return h->prot->url_get_multi_file_handle(h, handles, numhandles);
911}
912
913int ffurl_get_short_seek(void *urlcontext)
914{
915 URLContext *h = urlcontext;
916
917 if (!h || !h->prot || !h->prot->url_get_short_seek)
918 return AVERROR(ENOSYS);
919 return h->prot->url_get_short_seek(h);
920}
921
923{
924 if (!h || !h->prot || !h->prot->url_shutdown)
925 return AVERROR(ENOSYS);
926 return h->prot->url_shutdown(h, flags);
927}
928
930{
931 if (cb && cb->callback)
932 return cb->callback(cb->opaque);
933 return 0;
934}
935
936int ff_rename(const char *url_src, const char *url_dst, void *logctx)
937{
938 int ret = ffurl_move(url_src, url_dst);
939 if (ret < 0)
940 av_log(logctx, AV_LOG_ERROR, "failed to rename file %s to %s: %s\n", url_src, url_dst, av_err2str(ret));
941 return ret;
942}
static double val(void *priv, double ch)
Definition aeval.c:77
#define entry
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define D
Definition avdct.c:35
int ffurl_shutdown(URLContext *h, int flags)
Signal the URLContext that we are done reading or writing the stream.
Definition avio.c:922
int avio_handshake(AVIOContext *c)
Perform one step of the protocol handshake to accept a new client.
Definition avio.c:308
static void * urlcontext_child_next(void *obj, void *prev)
Definition avio.c:54
int ffio_fdopen(AVIOContext **sp, URLContext *h)
Create and initialize a AVIOContext for accessing the resource referenced by the URLContext h.
Definition avio.c:475
static const char * urlcontext_to_name(void *ptr)
Definition avio.c:45
int ffurl_handshake(URLContext *c)
Perform one step of the protocol handshake to accept a new client.
Definition avio.c:296
static void * avio_child_next(void *obj, void *prev)
Definition avio.c:83
#define URL_SCHEME_CHARS
Definition avio.c:314
int ffurl_alloc(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb)
Create a URLContext for accessing to the resource indicated by url, but do not initiate the connectio...
Definition avio.c:362
static const AVClass url_context_class
Definition avio.c:73
URLContext * ffio_geturlcontext(AVIOContext *s)
Return the URLContext associated with the AVIOContext.
Definition avio.c:113
static const AVOption avio_options[]
Definition avio.c:99
int ffio_open_whitelist(AVIOContext **s, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist)
Definition avio.c:558
static const AVClass * child_class_iterate(void **iter)
Definition avio.c:89
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition avio.c:929
int avio_close_dir(AVIODirContext **s)
Close directory.
Definition avio.c:851
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it.
Definition avio.c:466
static const AVOption urlcontext_options[]
Definition avio.c:65
int64_t ffurl_seek2(void *urlcontext, int64_t pos, int whence)
Definition avio.c:652
int ffurl_write2(void *urlcontext, const uint8_t *buf, int size)
Definition avio.c:639
int ffurl_read2(void *urlcontext, uint8_t *buf, int size)
Definition avio.c:623
int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
Open directory for reading.
Definition avio.c:800
int64_t ffurl_size(URLContext *h)
Return the filesize of the resource accessed by h, AVERROR(ENOSYS) if the operation is not supported ...
Definition avio.c:874
int ffurl_accept(URLContext *s, URLContext **c)
Accept an URLContext c on an URLContext s.
Definition avio.c:277
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition avio.c:663
#define NETWORK_IO_BUFFER_SIZE
Definition avio.c:41
int ffurl_close(URLContext *h)
Definition avio.c:686
int avio_accept(AVIOContext *s, AVIOContext **c)
Accept and allocate a client context on a server context.
Definition avio.c:285
static int url_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent, AVFormatContext *avfc)
Definition avio.c:402
static const struct URLProtocol * url_find_protocol(const char *filename)
Definition avio.c:319
#define AVIOOFFSET(x)
Definition avio.c:96
int ffurl_get_short_seek(void *urlcontext)
Return the current short seek threshold value for this URL.
Definition avio.c:913
void avio_free_directory_entry(AVIODirEntry **entry)
Free entry allocated by avio_read_dir().
Definition avio.c:866
static int retry_transfer_wrapper(URLContext *h, uint8_t *buf, const uint8_t *cbuf, int size, int size_min, int read)
Definition avio.c:578
int avio_check(const char *url, int flags)
Return AVIO_FLAG_* access flags corresponding to the access permissions of the resource in url,...
Definition avio.c:739
int ffio_open_whitelist2(AVIOContext **s, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, AVFormatContext *avfc)
Like ffio_open_whitelist(), but additionally records avfc on the underlying URLContext before it is c...
Definition avio.c:536
int ffurl_connect(URLContext *uc, AVDictionary **options)
Connect an URLContext that has been allocated by ffurl_alloc.
Definition avio.c:212
int avio_open(AVIOContext **s, const char *filename, int flags)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition avio.c:572
#define IO_BUFFER_SIZE
Definition avio.c:40
int ffurl_get_file_handle(URLContext *h)
Return the file descriptor associated with this URL.
Definition avio.c:889
const AVClass ff_avio_class
Definition avio.c:104
#define OFFSET(x)
Definition avio.c:62
int ffurl_get_multi_file_handle(URLContext *h, int **handles, int *numhandles)
Return the file descriptors associated with this URL.
Definition avio.c:896
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition avio.c:691
int ffurl_move(const char *url_src, const char *url_dst)
Move or rename a resource.
Definition avio.c:758
int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
Get next directory entry.
Definition avio.c:838
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition avio.c:724
int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
Read as many bytes as possible (up to size), calling the read function multiple times if necessary.
Definition avio.c:632
int avio_open2(AVIOContext **s, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition avio.c:566
int ffurl_delete(const char *url)
Delete a resource.
Definition avio.c:780
static int url_alloc_for_protocol(URLContext **puc, const URLProtocol *up, const char *filename, int flags, const AVIOInterruptCB *int_cb)
Definition avio.c:124
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
Definition avio.c:732
int ff_rename(const char *url_src, const char *url_dst, void *logctx)
Wrap ffurl_move() and log if error happens.
Definition avio.c:936
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
#define AVIO_FLAG_READ
read-only
Definition avio.h:617
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
Definition avio.h:468
#define AVIO_FLAG_WRITE
write-only
Definition avio.h:618
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, const uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
Definition aviobuf.c:109
#define AVIO_FLAG_DIRECT
Use direct mode.
Definition avio.h:644
#define AVIO_FLAG_READ_WRITE
read-write pseudo flag
Definition avio.h:619
void avio_context_free(AVIOContext **s)
Free the supplied IO context and everything associated with it.
Definition aviobuf.c:126
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition aviobuf.c:228
#define AVIO_SEEKABLE_TIME
Seeking by timestamp with avio_seek_time() is possible.
Definition avio.h:46
#define AVIO_FLAG_NONBLOCK
Use non-blocking mode.
Definition avio.h:636
#define AVSEEK_FORCE
OR'ing this flag into the "whence" parameter to a seek function causes it to seek by any means (like ...
Definition avio.h:476
static av_always_inline FFIOContext * ffiocontext(AVIOContext *ctx)
static uint32_t BS_FUNC read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public dictionary API.
const AVIOInterruptCB int_cb
Definition ffmpeg.c:322
const char * key
static int dummy
Definition ffplay.c:3764
#define fail
Definition test.h:479
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition opt.h:355
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition opt.h:351
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:233
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:60
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition error.h:58
#define AVERROR_PROTOCOL_NOT_FOUND
Protocol not found.
Definition error.h:65
#define AVERROR_EOF
End of file.
Definition error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AVERROR_OPTION_NOT_FOUND
Option not found.
Definition error.h:63
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition avstring.c:36
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition avstring.c:85
int av_match_list(const char *name, const char *list, char separator)
Check if a name is in a list.
Definition avstring.c:440
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition opt.c:2029
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition opt.c:1758
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition opt.c:891
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition opt.c:2219
int av_opt_set_dict(void *obj, AVDictionary **options)
Set all the options from a given dictionary on an object.
Definition opt.c:2066
const URLProtocol ff_libcurl_protocol
Definition libcurl.c:1263
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
static const char * obj
Definition mscl.c:57
int ff_network_init(void)
Initialize the network subsystem.
Definition network.c:63
void ff_network_close(void)
Definition network.c:121
#define av_strdup(s)
Definition ops_static.c:55
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
miscellaneous OS support macros and functions.
static int is_dos_path(const char *path)
Definition os_support.h:98
const URLProtocol ** ffurl_get_protocols(const char *whitelist, const char *blacklist)
Construct a list of protocols matching a given whitelist and/or blacklist.
Definition protocols.c:126
const AVClass * ff_urlcontext_child_class_iterate(void **iter)
Definition protocols.c:86
unsigned int pos
Definition spdifenc.c:431
Describe the class of an AVClass context structure.
Definition log.h:76
char * value
Definition dict.h:92
Format I/O context.
Definition avformat.h:1335
Bytestream IO Context.
Definition avio.h:160
struct URLContext * url_context
Definition avio.c:797
Describes single entry of the directory.
Definition avio.h:87
Callback for checking whether to abort blocking functions.
Definition avio.h:59
AVOption.
Definition opt.h:428
void * priv_data
Definition url.h:38
const char * protocol_whitelist
Definition url.h:47
int prefer_libcurl
route http(s) opens through the libcurl protocol
Definition url.h:51
AVIOInterruptCB interrupt_callback
Definition url.h:45
const struct URLProtocol * prot
Definition url.h:37
const AVClass * av_class
information for av_log().
Definition url.h:36
int is_streamed
true if streamed (no seek possible), default = false
Definition url.h:42
const char * protocol_blacklist
Definition url.h:48
int is_connected
Definition url.h:44
int uses_network
true if this protocol or one nested in it uses the network
Definition url.h:43
int max_packet_size
if non zero, the stream is packetized with this max packet size
Definition url.h:41
char * filename
specified URL
Definition url.h:39
int flags
Definition url.h:40
int(* url_open2)(URLContext *h, const char *url, int flags, AVDictionary **options)
This callback is to be used by protocols which open further nested protocols.
Definition url.h:62
int(* url_read)(URLContext *h, unsigned char *buf, int size)
Read data from the protocol.
Definition url.h:78
int(* url_open)(URLContext *h, const char *url, int flags)
Definition url.h:56
const AVClass * priv_data_class
Definition url.h:90
const char * default_whitelist
Definition url.h:99
const char * name
Definition url.h:55
int flags
Definition url.h:92
int(* url_write)(URLContext *h, const unsigned char *buf, int size)
Definition url.h:79
int priv_data_size
Definition url.h:91
int(* url_move)(URLContext *h_src, URLContext *h_dst)
Definition url.h:98
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
static char buffer[20]
Definition seek.c:32
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition time.c:93
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition time.c:57
int size
unbuffered private I/O API
#define URL_PROTOCOL_FLAG_NESTED_SCHEME
Definition url.h:32
static int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
Change the position that will be used by the next read/write operation on the resource accessed by h.
Definition url.h:225
#define URL_PROTOCOL_FLAG_NETWORK
Definition url.h:33
static double cb(void *priv, double x, double y)
Definition vf_geq.c:247
static const AVClass * child_class_iterate(void **iter)
Definition vf_scale.c:1041
int len
static double c[64]