2001-07-22 16:18:56 +02:00
|
|
|
/*
|
2011-10-30 17:56:57 +01:00
|
|
|
* unbuffered I/O
|
2002-05-26 00:34:32 +02:00
|
|
|
* Copyright (c) 2001 Fabrice Bellard
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2002-05-26 00:34:32 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 17:30:46 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2006-10-07 17:30:46 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2001-07-22 16:18:56 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2002-05-26 00:34:32 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-07-22 16:18:56 +02:00
|
|
|
*
|
2002-05-26 00:34:32 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 17:30:46 +02:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-12 23:43:26 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-07-22 16:18:56 +02:00
|
|
|
*/
|
2008-05-09 13:56:36 +02:00
|
|
|
|
2010-01-24 19:09:46 +01:00
|
|
|
#include <unistd.h>
|
2011-05-11 17:52:51 +02:00
|
|
|
|
2008-05-09 13:56:36 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2011-11-05 10:04:04 +01:00
|
|
|
#include "libavutil/dict.h"
|
2010-09-26 16:25:22 +02:00
|
|
|
#include "libavutil/opt.h"
|
2008-10-10 18:59:37 +02:00
|
|
|
#include "os_support.h"
|
2001-07-22 16:18:56 +02:00
|
|
|
#include "avformat.h"
|
2010-03-05 23:30:21 +01:00
|
|
|
#if CONFIG_NETWORK
|
|
|
|
#include "network.h"
|
|
|
|
#endif
|
2011-03-31 16:04:59 +02:00
|
|
|
#include "url.h"
|
2008-03-10 20:03:39 +01:00
|
|
|
|
2011-11-05 11:42:13 +01:00
|
|
|
static URLProtocol *first_protocol = NULL;
|
|
|
|
|
|
|
|
URLProtocol *ffurl_protocol_next(URLProtocol *prev)
|
|
|
|
{
|
|
|
|
return prev ? prev->next : first_protocol;
|
|
|
|
}
|
|
|
|
|
2008-03-10 20:03:39 +01:00
|
|
|
/** @name Logging context. */
|
|
|
|
/*@{*/
|
|
|
|
static const char *urlcontext_to_name(void *ptr)
|
|
|
|
{
|
|
|
|
URLContext *h = (URLContext *)ptr;
|
|
|
|
if(h->prot) return h->prot->name;
|
|
|
|
else return "NULL";
|
|
|
|
}
|
2011-11-05 10:04:04 +01:00
|
|
|
|
|
|
|
static void *urlcontext_child_next(void *obj, void *prev)
|
|
|
|
{
|
|
|
|
URLContext *h = obj;
|
|
|
|
if (!prev && h->priv_data && h->prot->priv_data_class)
|
|
|
|
return h->priv_data;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVClass *urlcontext_child_class_next(const AVClass *prev)
|
|
|
|
{
|
|
|
|
URLProtocol *p = NULL;
|
|
|
|
|
|
|
|
/* find the protocol that corresponds to prev */
|
|
|
|
while (prev && (p = ffurl_protocol_next(p)))
|
|
|
|
if (p->priv_data_class == prev)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* find next protocol with priv options */
|
|
|
|
while (p = ffurl_protocol_next(p))
|
|
|
|
if (p->priv_data_class)
|
|
|
|
return p->priv_data_class;
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-03-10 20:03:39 +01:00
|
|
|
static const AVOption options[] = {{NULL}};
|
2011-11-06 22:03:45 +01:00
|
|
|
const AVClass ffurl_context_class = {
|
2011-04-29 11:30:02 +02:00
|
|
|
.class_name = "URLContext",
|
|
|
|
.item_name = urlcontext_to_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
2011-11-05 10:04:04 +01:00
|
|
|
.child_next = urlcontext_child_next,
|
|
|
|
.child_class_next = urlcontext_child_class_next,
|
2011-04-29 11:30:02 +02:00
|
|
|
};
|
2008-03-10 20:03:39 +01:00
|
|
|
/*@}*/
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2003-07-17 12:25:36 +02:00
|
|
|
|
2011-11-07 11:17:50 +01:00
|
|
|
#if FF_API_OLD_INTERRUPT_CB
|
|
|
|
static int default_interrupt_cb(void);
|
2011-04-07 19:17:55 +02:00
|
|
|
int (*url_interrupt_cb)(void) = default_interrupt_cb;
|
2011-11-07 11:17:50 +01:00
|
|
|
#endif
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2007-12-12 20:01:58 +01:00
|
|
|
URLProtocol *av_protocol_next(URLProtocol *p)
|
|
|
|
{
|
2011-11-05 11:42:13 +01:00
|
|
|
return ffurl_protocol_next(p);
|
2007-12-12 20:01:58 +01:00
|
|
|
}
|
|
|
|
|
2011-04-04 08:18:54 +02:00
|
|
|
const char *avio_enum_protocols(void **opaque, int output)
|
|
|
|
{
|
|
|
|
URLProtocol **p = opaque;
|
2011-11-05 11:42:13 +01:00
|
|
|
*p = ffurl_protocol_next(*p);
|
2011-04-04 08:18:54 +02:00
|
|
|
if (!*p) return NULL;
|
|
|
|
if ((output && (*p)->url_write) || (!output && (*p)->url_read))
|
|
|
|
return (*p)->name;
|
|
|
|
return avio_enum_protocols(opaque, output);
|
2007-12-12 20:01:58 +01:00
|
|
|
}
|
|
|
|
|
2011-04-04 20:35:04 +02:00
|
|
|
int ffurl_register_protocol(URLProtocol *protocol, int size)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
|
|
|
URLProtocol **p;
|
2010-06-22 15:58:48 +02:00
|
|
|
if (size < sizeof(URLProtocol)) {
|
|
|
|
URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
|
|
|
|
memcpy(temp, protocol, size);
|
|
|
|
protocol = temp;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
p = &first_protocol;
|
|
|
|
while (*p != NULL) p = &(*p)->next;
|
|
|
|
*p = protocol;
|
|
|
|
protocol->next = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-22 16:03:37 +02:00
|
|
|
static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
|
2011-11-06 21:50:44 +01:00
|
|
|
const char *filename, int flags,
|
|
|
|
const AVIOInterruptCB *int_cb)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
|
|
|
URLContext *uc;
|
|
|
|
int err;
|
|
|
|
|
2010-03-05 23:30:21 +01:00
|
|
|
#if CONFIG_NETWORK
|
2011-12-30 10:43:10 +01:00
|
|
|
if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
|
2010-03-05 23:30:21 +01:00
|
|
|
return AVERROR(EIO);
|
|
|
|
#endif
|
2010-01-28 10:11:26 +01:00
|
|
|
uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
|
2002-07-24 19:50:23 +02:00
|
|
|
if (!uc) {
|
2007-02-13 19:26:14 +01:00
|
|
|
err = AVERROR(ENOMEM);
|
2002-07-24 19:50:23 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2011-11-06 22:03:45 +01:00
|
|
|
uc->av_class = &ffurl_context_class;
|
2007-01-14 23:07:19 +01:00
|
|
|
uc->filename = (char *) &uc[1];
|
2003-01-11 05:59:17 +01:00
|
|
|
strcpy(uc->filename, filename);
|
2001-07-22 16:18:56 +02:00
|
|
|
uc->prot = up;
|
|
|
|
uc->flags = flags;
|
|
|
|
uc->is_streamed = 0; /* default = not streamed */
|
2002-07-24 19:50:23 +02:00
|
|
|
uc->max_packet_size = 0; /* default: stream file */
|
2010-06-22 16:09:08 +02:00
|
|
|
if (up->priv_data_size) {
|
|
|
|
uc->priv_data = av_mallocz(up->priv_data_size);
|
|
|
|
if (up->priv_data_class) {
|
2011-12-23 23:59:10 +01:00
|
|
|
int proto_len= strlen(up->name);
|
2011-11-10 04:34:35 +01:00
|
|
|
char *start = strchr(uc->filename, ',');
|
2010-06-27 19:33:52 +02:00
|
|
|
*(const AVClass**)uc->priv_data = up->priv_data_class;
|
2010-06-22 16:09:08 +02:00
|
|
|
av_opt_set_defaults(uc->priv_data);
|
2011-12-23 23:59:10 +01:00
|
|
|
if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
|
2011-11-10 04:34:35 +01:00
|
|
|
int ret= 0;
|
|
|
|
char *p= start;
|
|
|
|
char sep= *++p;
|
|
|
|
char *key, *val;
|
|
|
|
p++;
|
|
|
|
while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
|
|
|
|
*val= *key= 0;
|
|
|
|
ret= av_opt_set(uc->priv_data, p, key+1, 0);
|
|
|
|
if (ret == AVERROR_OPTION_NOT_FOUND)
|
|
|
|
av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
|
|
|
|
*val= *key= sep;
|
|
|
|
p= val+1;
|
|
|
|
}
|
|
|
|
if(ret<0 || p!=key){
|
|
|
|
av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
|
|
|
|
av_freep(&uc->priv_data);
|
|
|
|
av_freep(&uc);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
memmove(start, key+1, strlen(key));
|
|
|
|
}
|
2010-06-22 16:09:08 +02:00
|
|
|
}
|
|
|
|
}
|
2011-11-06 21:50:44 +01:00
|
|
|
if (int_cb)
|
|
|
|
uc->interrupt_callback = *int_cb;
|
2008-05-05 11:17:56 +02:00
|
|
|
|
2001-07-22 16:18:56 +02:00
|
|
|
*puc = uc;
|
|
|
|
return 0;
|
2002-07-24 19:50:23 +02:00
|
|
|
fail:
|
|
|
|
*puc = NULL;
|
2010-03-05 23:30:21 +01:00
|
|
|
#if CONFIG_NETWORK
|
2011-12-30 10:43:10 +01:00
|
|
|
if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
|
|
|
|
ff_network_close();
|
2010-03-05 23:30:21 +01:00
|
|
|
#endif
|
2002-07-24 19:50:23 +02:00
|
|
|
return err;
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2011-11-05 10:04:04 +01:00
|
|
|
int ffurl_connect(URLContext* uc, AVDictionary **options)
|
2010-06-22 16:03:37 +02:00
|
|
|
{
|
2011-11-05 10:04:04 +01:00
|
|
|
int err =
|
|
|
|
#if !FF_API_OLD_AVIO
|
|
|
|
uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
|
|
|
|
#endif
|
|
|
|
uc->prot->url_open(uc, uc->filename, uc->flags);
|
2010-06-22 16:03:37 +02:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
uc->is_connected = 1;
|
2011-03-31 17:30:31 +02:00
|
|
|
//We must be careful here as ffurl_seek() could be slow, for example for http
|
2011-04-15 16:42:09 +02:00
|
|
|
if( (uc->flags & AVIO_FLAG_WRITE)
|
2010-06-22 16:03:37 +02:00
|
|
|
|| !strcmp(uc->prot->name, "file"))
|
2011-03-31 17:30:31 +02:00
|
|
|
if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
|
2010-06-22 16:03:37 +02:00
|
|
|
uc->is_streamed= 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-31 16:00:49 +02:00
|
|
|
#if FF_API_OLD_AVIO
|
2010-06-22 16:03:37 +02:00
|
|
|
int url_open_protocol (URLContext **puc, struct URLProtocol *up,
|
|
|
|
const char *filename, int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2011-11-06 21:50:44 +01:00
|
|
|
ret = url_alloc_for_protocol(puc, up, filename, flags, NULL);
|
2010-06-22 16:03:37 +02:00
|
|
|
if (ret)
|
|
|
|
goto fail;
|
2011-11-05 10:04:04 +01:00
|
|
|
ret = ffurl_connect(*puc, NULL);
|
2010-06-22 16:03:37 +02:00
|
|
|
if (!ret)
|
|
|
|
return 0;
|
|
|
|
fail:
|
2011-03-31 17:36:06 +02:00
|
|
|
ffurl_close(*puc);
|
2010-06-22 16:03:37 +02:00
|
|
|
*puc = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
2011-03-31 16:04:59 +02:00
|
|
|
int url_alloc(URLContext **puc, const char *filename, int flags)
|
|
|
|
{
|
2011-11-06 21:50:44 +01:00
|
|
|
return ffurl_alloc(puc, filename, flags, NULL);
|
2011-03-31 16:04:59 +02:00
|
|
|
}
|
2011-03-31 16:10:50 +02:00
|
|
|
int url_connect(URLContext* uc)
|
|
|
|
{
|
2011-11-05 10:04:04 +01:00
|
|
|
return ffurl_connect(uc, NULL);
|
2011-03-31 16:10:50 +02:00
|
|
|
}
|
2011-03-31 16:25:10 +02:00
|
|
|
int url_open(URLContext **puc, const char *filename, int flags)
|
|
|
|
{
|
2011-11-05 10:04:04 +01:00
|
|
|
return ffurl_open(puc, filename, flags, NULL, NULL);
|
2011-03-31 16:25:10 +02:00
|
|
|
}
|
2011-03-31 16:31:43 +02:00
|
|
|
int url_read(URLContext *h, unsigned char *buf, int size)
|
|
|
|
{
|
|
|
|
return ffurl_read(h, buf, size);
|
|
|
|
}
|
2011-03-31 16:40:31 +02:00
|
|
|
int url_read_complete(URLContext *h, unsigned char *buf, int size)
|
|
|
|
{
|
|
|
|
return ffurl_read_complete(h, buf, size);
|
|
|
|
}
|
2011-03-31 16:48:01 +02:00
|
|
|
int url_write(URLContext *h, const unsigned char *buf, int size)
|
|
|
|
{
|
|
|
|
return ffurl_write(h, buf, size);
|
|
|
|
}
|
2011-03-31 17:30:31 +02:00
|
|
|
int64_t url_seek(URLContext *h, int64_t pos, int whence)
|
|
|
|
{
|
|
|
|
return ffurl_seek(h, pos, whence);
|
|
|
|
}
|
2011-03-31 17:36:06 +02:00
|
|
|
int url_close(URLContext *h)
|
|
|
|
{
|
|
|
|
return ffurl_close(h);
|
|
|
|
}
|
2011-03-31 17:46:00 +02:00
|
|
|
int64_t url_filesize(URLContext *h)
|
|
|
|
{
|
|
|
|
return ffurl_size(h);
|
|
|
|
}
|
2011-03-31 17:51:24 +02:00
|
|
|
int url_get_file_handle(URLContext *h)
|
|
|
|
{
|
|
|
|
return ffurl_get_file_handle(h);
|
|
|
|
}
|
2011-03-31 17:58:04 +02:00
|
|
|
int url_get_max_packet_size(URLContext *h)
|
|
|
|
{
|
|
|
|
return h->max_packet_size;
|
|
|
|
}
|
2011-03-31 18:00:55 +02:00
|
|
|
void url_get_filename(URLContext *h, char *buf, int buf_size)
|
|
|
|
{
|
|
|
|
av_strlcpy(buf, h->filename, buf_size);
|
|
|
|
}
|
2011-04-04 20:15:44 +02:00
|
|
|
void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
|
|
|
|
{
|
|
|
|
avio_set_interrupt_cb(interrupt_cb);
|
|
|
|
}
|
2011-04-04 20:35:04 +02:00
|
|
|
int av_register_protocol2(URLProtocol *protocol, int size)
|
|
|
|
{
|
|
|
|
return ffurl_register_protocol(protocol, size);
|
|
|
|
}
|
2011-03-31 16:00:49 +02:00
|
|
|
#endif
|
2010-06-22 16:03:37 +02:00
|
|
|
|
2010-07-18 20:38:23 +02:00
|
|
|
#define URL_SCHEME_CHARS \
|
|
|
|
"abcdefghijklmnopqrstuvwxyz" \
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
|
|
|
"0123456789+-."
|
|
|
|
|
2011-11-06 21:50:44 +01:00
|
|
|
int ffurl_alloc(URLContext **puc, const char *filename, int flags,
|
|
|
|
const AVIOInterruptCB *int_cb)
|
2008-08-20 01:44:23 +02:00
|
|
|
{
|
2011-11-05 11:42:13 +01:00
|
|
|
URLProtocol *up = NULL;
|
2011-02-28 14:39:36 +01:00
|
|
|
char proto_str[128], proto_nested[128], *ptr;
|
2011-11-24 15:03:27 +01:00
|
|
|
size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
|
2010-07-18 20:38:23 +02:00
|
|
|
|
2011-11-07 04:18:53 +01:00
|
|
|
if (!first_protocol) {
|
|
|
|
av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
|
|
|
|
"Missing call to av_register_all()?\n");
|
|
|
|
}
|
|
|
|
|
2011-11-24 15:03:27 +01:00
|
|
|
if (filename[proto_len] != ':' && filename[proto_len] != ',' || is_dos_path(filename))
|
2008-08-20 01:44:23 +02:00
|
|
|
strcpy(proto_str, "file");
|
2010-07-18 20:38:23 +02:00
|
|
|
else
|
2011-11-24 15:03:27 +01:00
|
|
|
av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
|
2008-08-20 01:44:23 +02:00
|
|
|
|
2011-11-10 04:34:35 +01:00
|
|
|
if ((ptr = strchr(proto_str, ',')))
|
|
|
|
*ptr = '\0';
|
2011-02-28 14:39:36 +01:00
|
|
|
av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
|
|
|
|
if ((ptr = strchr(proto_nested, '+')))
|
|
|
|
*ptr = '\0';
|
|
|
|
|
2011-11-05 11:42:13 +01:00
|
|
|
while (up = ffurl_protocol_next(up)) {
|
2008-08-20 01:44:23 +02:00
|
|
|
if (!strcmp(proto_str, up->name))
|
2011-11-06 21:50:44 +01:00
|
|
|
return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
|
2011-02-28 14:39:36 +01:00
|
|
|
if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
|
|
|
|
!strcmp(proto_nested, up->name))
|
2011-11-06 21:50:44 +01:00
|
|
|
return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
|
2008-08-20 01:44:23 +02:00
|
|
|
}
|
|
|
|
*puc = NULL;
|
|
|
|
return AVERROR(ENOENT);
|
|
|
|
}
|
|
|
|
|
2011-11-06 21:50:44 +01:00
|
|
|
int ffurl_open(URLContext **puc, const char *filename, int flags,
|
2011-11-05 10:04:04 +01:00
|
|
|
const AVIOInterruptCB *int_cb, AVDictionary **options)
|
2010-06-22 16:03:37 +02:00
|
|
|
{
|
2011-11-06 21:50:44 +01:00
|
|
|
int ret = ffurl_alloc(puc, filename, flags, int_cb);
|
2010-06-22 16:03:37 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2011-11-05 10:04:04 +01:00
|
|
|
if (options && (*puc)->prot->priv_data_class &&
|
|
|
|
(ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
|
|
|
|
goto fail;
|
|
|
|
ret = ffurl_connect(*puc, options);
|
2010-06-22 16:03:37 +02:00
|
|
|
if (!ret)
|
|
|
|
return 0;
|
2011-11-05 10:04:04 +01:00
|
|
|
fail:
|
2011-03-31 17:36:06 +02:00
|
|
|
ffurl_close(*puc);
|
2010-06-22 16:03:37 +02:00
|
|
|
*puc = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-02-04 19:12:37 +01:00
|
|
|
static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
|
2010-10-06 13:18:38 +02:00
|
|
|
int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
|
2009-06-04 08:25:53 +02:00
|
|
|
{
|
|
|
|
int ret, len;
|
2010-01-24 19:09:46 +01:00
|
|
|
int fast_retries = 5;
|
2009-06-04 08:25:53 +02:00
|
|
|
|
|
|
|
len = 0;
|
2011-02-04 19:12:37 +01:00
|
|
|
while (len < size_min) {
|
2010-10-06 13:18:38 +02:00
|
|
|
ret = transfer_func(h, buf+len, size-len);
|
2011-02-04 19:12:37 +01:00
|
|
|
if (ret == AVERROR(EINTR))
|
|
|
|
continue;
|
2011-04-04 20:11:19 +02:00
|
|
|
if (h->flags & AVIO_FLAG_NONBLOCK)
|
2011-02-04 19:12:37 +01:00
|
|
|
return ret;
|
2010-01-23 11:23:47 +01:00
|
|
|
if (ret == AVERROR(EAGAIN)) {
|
|
|
|
ret = 0;
|
2010-01-24 19:09:46 +01:00
|
|
|
if (fast_retries)
|
|
|
|
fast_retries--;
|
|
|
|
else
|
|
|
|
usleep(1000);
|
2010-01-23 11:23:47 +01:00
|
|
|
} else if (ret < 1)
|
|
|
|
return ret < 0 ? ret : len;
|
2010-01-24 19:09:46 +01:00
|
|
|
if (ret)
|
|
|
|
fast_retries = FFMAX(fast_retries, 2);
|
2009-06-04 08:25:53 +02:00
|
|
|
len += ret;
|
2011-11-13 23:43:12 +01:00
|
|
|
if (len < size && ff_check_interrupt(&h->interrupt_callback))
|
2011-03-13 00:42:27 +01:00
|
|
|
return AVERROR_EXIT;
|
2009-06-04 08:25:53 +02:00
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2011-03-31 16:31:43 +02:00
|
|
|
int ffurl_read(URLContext *h, unsigned char *buf, int size)
|
2011-02-04 19:12:37 +01:00
|
|
|
{
|
2011-04-20 18:13:54 +02:00
|
|
|
if (!(h->flags & AVIO_FLAG_READ))
|
2011-02-04 19:12:37 +01:00
|
|
|
return AVERROR(EIO);
|
|
|
|
return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
|
|
|
|
}
|
|
|
|
|
2011-03-31 16:40:31 +02:00
|
|
|
int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
|
2010-10-06 13:18:38 +02:00
|
|
|
{
|
2011-04-20 18:13:54 +02:00
|
|
|
if (!(h->flags & AVIO_FLAG_READ))
|
2011-02-04 19:12:37 +01:00
|
|
|
return AVERROR(EIO);
|
|
|
|
return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
|
2010-10-06 13:18:38 +02:00
|
|
|
}
|
|
|
|
|
2011-03-31 16:48:01 +02:00
|
|
|
int ffurl_write(URLContext *h, const unsigned char *buf, int size)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2011-04-15 16:42:09 +02:00
|
|
|
if (!(h->flags & AVIO_FLAG_WRITE))
|
2007-07-19 17:23:32 +02:00
|
|
|
return AVERROR(EIO);
|
2002-07-24 19:50:23 +02:00
|
|
|
/* avoid sending too big packets */
|
|
|
|
if (h->max_packet_size && size > h->max_packet_size)
|
2007-07-19 17:23:32 +02:00
|
|
|
return AVERROR(EIO);
|
2010-10-06 13:18:43 +02:00
|
|
|
|
2011-04-24 17:46:50 +02:00
|
|
|
return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
|
2001-07-22 16:18:56 +02:00
|
|
|
}
|
|
|
|
|
2011-03-31 17:30:31 +02:00
|
|
|
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t ret;
|
2001-07-22 16:18:56 +02:00
|
|
|
|
|
|
|
if (!h->prot->url_seek)
|
2010-04-18 19:37:16 +02:00
|
|
|
return AVERROR(ENOSYS);
|
2010-03-15 23:54:22 +01:00
|
|
|
ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
|
2001-07-22 16:18:56 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-03-31 17:36:06 +02:00
|
|
|
int ffurl_close(URLContext *h)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2007-11-16 01:14:48 +01:00
|
|
|
int ret = 0;
|
2011-03-31 16:25:10 +02:00
|
|
|
if (!h) return 0; /* can happen when ffurl_open fails */
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2010-06-22 16:03:37 +02:00
|
|
|
if (h->is_connected && h->prot->url_close)
|
2007-11-16 01:14:48 +01:00
|
|
|
ret = h->prot->url_close(h);
|
2010-03-05 23:30:21 +01:00
|
|
|
#if CONFIG_NETWORK
|
2011-12-30 10:43:10 +01:00
|
|
|
if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
|
|
|
|
ff_network_close();
|
2010-03-05 23:30:21 +01:00
|
|
|
#endif
|
2011-11-08 23:48:40 +01:00
|
|
|
if (h->prot->priv_data_size) {
|
|
|
|
if (h->prot->priv_data_class)
|
|
|
|
av_opt_free(h->priv_data);
|
2010-06-22 16:09:08 +02:00
|
|
|
av_free(h->priv_data);
|
2011-11-08 23:48:40 +01:00
|
|
|
}
|
2002-05-19 01:11:09 +02:00
|
|
|
av_free(h);
|
2001-07-22 16:18:56 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-12 09:46:45 +02:00
|
|
|
#if FF_API_OLD_AVIO
|
2001-07-22 16:18:56 +02:00
|
|
|
int url_exist(const char *filename)
|
|
|
|
{
|
|
|
|
URLContext *h;
|
2011-11-05 10:04:04 +01:00
|
|
|
if (ffurl_open(&h, filename, AVIO_FLAG_READ, NULL, NULL) < 0)
|
2001-07-22 16:18:56 +02:00
|
|
|
return 0;
|
2011-03-31 17:36:06 +02:00
|
|
|
ffurl_close(h);
|
2001-07-22 16:18:56 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2011-04-12 09:46:45 +02:00
|
|
|
#endif
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2011-04-08 18:32:25 +02:00
|
|
|
int avio_check(const char *url, int flags)
|
|
|
|
{
|
|
|
|
URLContext *h;
|
2011-11-06 21:50:44 +01:00
|
|
|
int ret = ffurl_alloc(&h, url, flags, NULL);
|
2011-04-08 18:32:25 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (h->prot->url_check) {
|
|
|
|
ret = h->prot->url_check(h, flags);
|
|
|
|
} else {
|
2011-11-05 10:04:04 +01:00
|
|
|
ret = ffurl_connect(h, NULL);
|
2011-04-08 18:32:25 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
ret = flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
ffurl_close(h);
|
|
|
|
return ret;
|
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
|
2011-03-31 17:46:00 +02:00
|
|
|
int64_t ffurl_size(URLContext *h)
|
2001-07-22 16:18:56 +02:00
|
|
|
{
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t pos, size;
|
2005-12-17 19:14:38 +01:00
|
|
|
|
2011-03-31 17:30:31 +02:00
|
|
|
size= ffurl_seek(h, 0, AVSEEK_SIZE);
|
2007-01-01 22:49:09 +01:00
|
|
|
if(size<0){
|
2011-03-31 17:30:31 +02:00
|
|
|
pos = ffurl_seek(h, 0, SEEK_CUR);
|
|
|
|
if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
|
2007-01-30 11:37:52 +01:00
|
|
|
return size;
|
|
|
|
size++;
|
2011-03-31 17:30:31 +02:00
|
|
|
ffurl_seek(h, pos, SEEK_SET);
|
2007-01-01 22:49:09 +01:00
|
|
|
}
|
2001-07-22 16:18:56 +02:00
|
|
|
return size;
|
|
|
|
}
|
2002-07-24 19:50:23 +02:00
|
|
|
|
2011-03-31 17:51:24 +02:00
|
|
|
int ffurl_get_file_handle(URLContext *h)
|
2009-03-03 18:04:51 +01:00
|
|
|
{
|
|
|
|
if (!h->prot->url_get_file_handle)
|
|
|
|
return -1;
|
|
|
|
return h->prot->url_get_file_handle(h);
|
|
|
|
}
|
|
|
|
|
2011-11-07 11:17:50 +01:00
|
|
|
#if FF_API_OLD_INTERRUPT_CB
|
2003-07-17 12:25:36 +02:00
|
|
|
static int default_interrupt_cb(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-07 19:17:55 +02:00
|
|
|
void avio_set_interrupt_cb(int (*interrupt_cb)(void))
|
2003-07-17 12:25:36 +02:00
|
|
|
{
|
|
|
|
if (!interrupt_cb)
|
|
|
|
interrupt_cb = default_interrupt_cb;
|
|
|
|
url_interrupt_cb = interrupt_cb;
|
|
|
|
}
|
2011-11-07 11:17:50 +01:00
|
|
|
#endif
|
2007-11-24 08:09:32 +01:00
|
|
|
|
2011-11-06 21:10:21 +01:00
|
|
|
int ff_check_interrupt(AVIOInterruptCB *cb)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
|
|
|
|
return ret;
|
2011-11-07 11:17:50 +01:00
|
|
|
#if FF_API_OLD_INTERRUPT_CB
|
2011-11-06 21:10:21 +01:00
|
|
|
return url_interrupt_cb();
|
2011-11-07 11:17:50 +01:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2011-11-06 21:10:21 +01:00
|
|
|
}
|
|
|
|
|
2011-04-04 19:46:19 +02:00
|
|
|
#if FF_API_OLD_AVIO
|
2007-12-19 21:57:13 +01:00
|
|
|
int av_url_read_pause(URLContext *h, int pause)
|
2007-11-24 08:09:32 +01:00
|
|
|
{
|
|
|
|
if (!h->prot->url_read_pause)
|
|
|
|
return AVERROR(ENOSYS);
|
2007-12-19 21:57:13 +01:00
|
|
|
return h->prot->url_read_pause(h, pause);
|
2007-11-24 08:09:32 +01:00
|
|
|
}
|
|
|
|
|
2008-10-03 12:16:29 +02:00
|
|
|
int64_t av_url_read_seek(URLContext *h,
|
2007-11-24 08:09:32 +01:00
|
|
|
int stream_index, int64_t timestamp, int flags)
|
|
|
|
{
|
|
|
|
if (!h->prot->url_read_seek)
|
|
|
|
return AVERROR(ENOSYS);
|
|
|
|
return h->prot->url_read_seek(h, stream_index, timestamp, flags);
|
|
|
|
}
|
2011-04-04 19:48:31 +02:00
|
|
|
#endif
|