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