de6d9b64 |
/*
* Unbuffered io for ffmpeg system |
19720f15 |
* Copyright (c) 2001 Fabrice Bellard |
de6d9b64 |
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
19720f15 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
de6d9b64 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
de6d9b64 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19720f15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. |
de6d9b64 |
* |
19720f15 |
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
de6d9b64 |
*/ |
245976da |
|
f1d2b572 |
#include <unistd.h> |
046f081b |
|
245976da |
#include "libavutil/avstring.h" |
6ed04040 |
#include "libavutil/opt.h" |
bc574408 |
#include "os_support.h" |
de6d9b64 |
#include "avformat.h" |
67d4b3f2 |
#if CONFIG_NETWORK
#include "network.h"
#endif |
5652bb94 |
#include "url.h" |
5acef35f |
/** @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";
}
static const AVOption options[] = {{NULL}}; |
4a7a1b7d |
static const AVClass urlcontext_class = {
.class_name = "URLContext",
.item_name = urlcontext_to_name,
.option = options,
.version = LIBAVUTIL_VERSION_INT,
}; |
5acef35f |
/*@}*/ |
de6d9b64 |
|
019ac05a |
static int default_interrupt_cb(void);
|
de6d9b64 |
URLProtocol *first_protocol = NULL; |
026e1757 |
int (*url_interrupt_cb)(void) = default_interrupt_cb; |
de6d9b64 |
|
84be6e72 |
URLProtocol *av_protocol_next(URLProtocol *p)
{
if(p) return p->next;
else return first_protocol;
}
|
f8270bbf |
const char *avio_enum_protocols(void **opaque, int output)
{ |
06a9da73 |
URLProtocol *p = *opaque;
p = p ? p->next : first_protocol;
if (!p) return NULL;
if ((output && p->url_write) || (!output && p->url_read))
return p->name; |
f8270bbf |
return avio_enum_protocols(opaque, output); |
84be6e72 |
}
|
8e76a19b |
int ffurl_register_protocol(URLProtocol *protocol, int size) |
de6d9b64 |
{
URLProtocol **p; |
9b07a2dc |
if (size < sizeof(URLProtocol)) {
URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
memcpy(temp, protocol, size);
protocol = temp;
} |
de6d9b64 |
p = &first_protocol;
while (*p != NULL) p = &(*p)->next;
*p = protocol;
protocol->next = NULL;
return 0;
}
|
ffbb289a |
static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up, |
02174293 |
const char *filename, int flags) |
de6d9b64 |
{
URLContext *uc;
int err;
|
67d4b3f2 |
#if CONFIG_NETWORK
if (!ff_network_init())
return AVERROR(EIO);
#endif |
31277aeb |
uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1); |
8a9488b5 |
if (!uc) { |
8fa36ae0 |
err = AVERROR(ENOMEM); |
8a9488b5 |
goto fail;
} |
5acef35f |
uc->av_class = &urlcontext_class; |
62181004 |
uc->filename = (char *) &uc[1]; |
f746a046 |
strcpy(uc->filename, filename); |
de6d9b64 |
uc->prot = up;
uc->flags = flags;
uc->is_streamed = 0; /* default = not streamed */ |
8a9488b5 |
uc->max_packet_size = 0; /* default: stream file */ |
735cf6b2 |
if (up->priv_data_size) {
uc->priv_data = av_mallocz(up->priv_data_size);
if (up->priv_data_class) { |
805488f0 |
*(const AVClass**)uc->priv_data = up->priv_data_class; |
735cf6b2 |
av_opt_set_defaults(uc->priv_data);
}
} |
abbae514 |
|
de6d9b64 |
*puc = uc;
return 0; |
8a9488b5 |
fail:
*puc = NULL; |
67d4b3f2 |
#if CONFIG_NETWORK
ff_network_close();
#endif |
8a9488b5 |
return err; |
de6d9b64 |
}
|
62eaaeac |
int ffurl_connect(URLContext* uc) |
ffbb289a |
{
int err = uc->prot->url_open(uc, uc->filename, uc->flags);
if (err)
return err;
uc->is_connected = 1; |
58a48c65 |
//We must be careful here as ffurl_seek() could be slow, for example for http |
59d96941 |
if( (uc->flags & AVIO_FLAG_WRITE) |
ffbb289a |
|| !strcmp(uc->prot->name, "file")) |
58a48c65 |
if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0) |
ffbb289a |
uc->is_streamed= 1;
return 0;
}
|
333e8943 |
#if FF_API_OLD_AVIO |
ffbb289a |
int url_open_protocol (URLContext **puc, struct URLProtocol *up,
const char *filename, int flags)
{
int ret;
ret = url_alloc_for_protocol(puc, up, filename, flags);
if (ret)
goto fail; |
62eaaeac |
ret = ffurl_connect(*puc); |
ffbb289a |
if (!ret)
return 0;
fail: |
e52a9145 |
ffurl_close(*puc); |
ffbb289a |
*puc = NULL;
return ret;
} |
5652bb94 |
int url_alloc(URLContext **puc, const char *filename, int flags)
{
return ffurl_alloc(puc, filename, flags);
} |
62eaaeac |
int url_connect(URLContext* uc)
{
return ffurl_connect(uc);
} |
0589da0a |
int url_open(URLContext **puc, const char *filename, int flags)
{
return ffurl_open(puc, filename, flags);
} |
bc371aca |
int url_read(URLContext *h, unsigned char *buf, int size)
{
return ffurl_read(h, buf, size);
} |
dce37564 |
int url_read_complete(URLContext *h, unsigned char *buf, int size)
{
return ffurl_read_complete(h, buf, size);
} |
925e908b |
int url_write(URLContext *h, const unsigned char *buf, int size)
{
return ffurl_write(h, buf, size);
} |
58a48c65 |
int64_t url_seek(URLContext *h, int64_t pos, int whence)
{
return ffurl_seek(h, pos, whence);
} |
e52a9145 |
int url_close(URLContext *h)
{
return ffurl_close(h);
} |
32a97d46 |
int64_t url_filesize(URLContext *h)
{
return ffurl_size(h);
} |
1869ea03 |
int url_get_file_handle(URLContext *h)
{
return ffurl_get_file_handle(h);
} |
5958df34 |
int url_get_max_packet_size(URLContext *h)
{
return h->max_packet_size;
} |
727c7aa0 |
void url_get_filename(URLContext *h, char *buf, int buf_size)
{
av_strlcpy(buf, h->filename, buf_size);
} |
80c6e238 |
void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
{
avio_set_interrupt_cb(interrupt_cb);
} |
8e76a19b |
int av_register_protocol2(URLProtocol *protocol, int size)
{
return ffurl_register_protocol(protocol, size);
} |
333e8943 |
#endif |
ffbb289a |
|
2bab5d3e |
#define URL_SCHEME_CHARS \
"abcdefghijklmnopqrstuvwxyz" \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"0123456789+-."
|
5652bb94 |
int ffurl_alloc(URLContext **puc, const char *filename, int flags) |
ba99cfc2 |
{
URLProtocol *up; |
8f73c060 |
char proto_str[128], proto_nested[128], *ptr; |
2bab5d3e |
size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
if (filename[proto_len] != ':' || is_dos_path(filename)) |
ba99cfc2 |
strcpy(proto_str, "file"); |
2bab5d3e |
else
av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str))); |
ba99cfc2 |
|
8f73c060 |
av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
if ((ptr = strchr(proto_nested, '+')))
*ptr = '\0';
|
ba99cfc2 |
up = first_protocol;
while (up != NULL) {
if (!strcmp(proto_str, up->name)) |
ffbb289a |
return url_alloc_for_protocol (puc, up, filename, flags); |
8f73c060 |
if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
!strcmp(proto_nested, up->name))
return url_alloc_for_protocol (puc, up, filename, flags); |
ba99cfc2 |
up = up->next;
}
*puc = NULL;
return AVERROR(ENOENT);
}
|
0589da0a |
int ffurl_open(URLContext **puc, const char *filename, int flags) |
ffbb289a |
{ |
5652bb94 |
int ret = ffurl_alloc(puc, filename, flags); |
ffbb289a |
if (ret)
return ret; |
62eaaeac |
ret = ffurl_connect(*puc); |
ffbb289a |
if (!ret)
return 0; |
e52a9145 |
ffurl_close(*puc); |
ffbb289a |
*puc = NULL;
return ret;
}
|
90441276 |
static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min, |
a46f7516 |
int (*transfer_func)(URLContext *h, unsigned char *buf, int size)) |
0e848977 |
{
int ret, len; |
f1d2b572 |
int fast_retries = 5; |
0e848977 |
len = 0; |
90441276 |
while (len < size_min) { |
a46f7516 |
ret = transfer_func(h, buf+len, size-len); |
90441276 |
if (ret == AVERROR(EINTR))
continue; |
f87b1b37 |
if (h->flags & AVIO_FLAG_NONBLOCK) |
90441276 |
return ret; |
ddb901b7 |
if (ret == AVERROR(EAGAIN)) {
ret = 0; |
f1d2b572 |
if (fast_retries)
fast_retries--;
else
usleep(1000); |
ddb901b7 |
} else if (ret < 1)
return ret < 0 ? ret : len; |
f1d2b572 |
if (ret)
fast_retries = FFMAX(fast_retries, 2); |
0e848977 |
len += ret; |
6114bffa |
if (len < size && url_interrupt_cb()) |
c76374c6 |
return AVERROR_EXIT; |
0e848977 |
}
return len;
}
|
bc371aca |
int ffurl_read(URLContext *h, unsigned char *buf, int size) |
90441276 |
{ |
b9553cf4 |
if (!(h->flags & AVIO_FLAG_READ)) |
90441276 |
return AVERROR(EIO);
return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
}
|
dce37564 |
int ffurl_read_complete(URLContext *h, unsigned char *buf, int size) |
a46f7516 |
{ |
b9553cf4 |
if (!(h->flags & AVIO_FLAG_READ)) |
90441276 |
return AVERROR(EIO);
return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read); |
a46f7516 |
}
|
925e908b |
int ffurl_write(URLContext *h, const unsigned char *buf, int size) |
de6d9b64 |
{ |
59d96941 |
if (!(h->flags & AVIO_FLAG_WRITE)) |
6f3e0b21 |
return AVERROR(EIO); |
8a9488b5 |
/* avoid sending too big packets */
if (h->max_packet_size && size > h->max_packet_size) |
6f3e0b21 |
return AVERROR(EIO); |
fe5feaeb |
|
e3323a1c |
return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write); |
de6d9b64 |
}
|
58a48c65 |
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence) |
de6d9b64 |
{ |
bc5c918e |
int64_t ret; |
de6d9b64 |
if (!h->prot->url_seek) |
28894105 |
return AVERROR(ENOSYS); |
493f54ad |
ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE); |
de6d9b64 |
return ret;
}
|
e52a9145 |
int ffurl_close(URLContext *h) |
de6d9b64 |
{ |
7a813b36 |
int ret = 0; |
0589da0a |
if (!h) return 0; /* can happen when ffurl_open fails */ |
de6d9b64 |
|
ffbb289a |
if (h->is_connected && h->prot->url_close) |
7a813b36 |
ret = h->prot->url_close(h); |
67d4b3f2 |
#if CONFIG_NETWORK
ff_network_close();
#endif |
735cf6b2 |
if (h->prot->priv_data_size)
av_free(h->priv_data); |
1ea4f593 |
av_free(h); |
de6d9b64 |
return ret;
}
|
4bde56d1 |
#if FF_API_OLD_AVIO |
de6d9b64 |
int url_exist(const char *filename)
{
URLContext *h; |
59d96941 |
if (ffurl_open(&h, filename, AVIO_FLAG_READ) < 0) |
de6d9b64 |
return 0; |
e52a9145 |
ffurl_close(h); |
de6d9b64 |
return 1;
} |
4bde56d1 |
#endif |
de6d9b64 |
|
175389c8 |
int avio_check(const char *url, int flags)
{
URLContext *h;
int ret = ffurl_alloc(&h, url, flags);
if (ret)
return ret;
if (h->prot->url_check) {
ret = h->prot->url_check(h, flags);
} else {
ret = ffurl_connect(h);
if (ret >= 0)
ret = flags;
}
ffurl_close(h);
return ret;
} |
de6d9b64 |
|
32a97d46 |
int64_t ffurl_size(URLContext *h) |
de6d9b64 |
{ |
bc5c918e |
int64_t pos, size; |
115329f1 |
|
58a48c65 |
size= ffurl_seek(h, 0, AVSEEK_SIZE); |
8e287af0 |
if(size<0){ |
58a48c65 |
pos = ffurl_seek(h, 0, SEEK_CUR);
if ((size = ffurl_seek(h, -1, SEEK_END)) < 0) |
eabbae73 |
return size;
size++; |
58a48c65 |
ffurl_seek(h, pos, SEEK_SET); |
8e287af0 |
} |
de6d9b64 |
return size;
} |
8a9488b5 |
|
1869ea03 |
int ffurl_get_file_handle(URLContext *h) |
f0a80394 |
{
if (!h->prot->url_get_file_handle)
return -1;
return h->prot->url_get_file_handle(h);
}
|
019ac05a |
static int default_interrupt_cb(void)
{
return 0;
}
|
026e1757 |
void avio_set_interrupt_cb(int (*interrupt_cb)(void)) |
019ac05a |
{
if (!interrupt_cb)
interrupt_cb = default_interrupt_cb;
url_interrupt_cb = interrupt_cb;
} |
536333a0 |
|
fa104e14 |
#if FF_API_OLD_AVIO |
502bdf68 |
int av_url_read_pause(URLContext *h, int pause) |
536333a0 |
{
if (!h->prot->url_read_pause)
return AVERROR(ENOSYS); |
502bdf68 |
return h->prot->url_read_pause(h, pause); |
536333a0 |
}
|
bc5c918e |
int64_t av_url_read_seek(URLContext *h, |
536333a0 |
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);
} |
1305d93c |
#endif |