From f4c04dbcdcdc48f8ff8f74719c5beb3929c1bd0f Mon Sep 17 00:00:00 2001 From: x Date: Wed, 10 Oct 2012 01:02:03 +0200 Subject: [PATCH] lavf/file: WIP loop --- libavformat/file.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/libavformat/file.c b/libavformat/file.c index 209957b..07dbd6d 100644 --- a/libavformat/file.c +++ b/libavformat/file.c @@ -49,10 +49,14 @@ typedef struct FileContext { const AVClass *class; int fd; int trunc; + int loop; + const char *fname; + int flags; } FileContext; static const AVOption file_options[] = { { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM }, + { "fileloop", "loop / FIXME desc", offsetof(FileContext, loop), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM }, { NULL } }; @@ -63,13 +67,6 @@ static const AVClass file_class = { .version = LIBAVUTIL_VERSION_INT, }; -static int file_read(URLContext *h, unsigned char *buf, int size) -{ - FileContext *c = h->priv_data; - int r = read(c->fd, buf, size); - return (-1 == r)?AVERROR(errno):r; -} - static int file_write(URLContext *h, const unsigned char *buf, int size) { FileContext *c = h->priv_data; @@ -105,6 +102,8 @@ static int file_open(URLContext *h, const char *filename, int flags) int fd; struct stat st; + c->fname = filename; + c->flags = flags; av_strstart(filename, "file:", &filename); if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) { @@ -127,6 +126,8 @@ static int file_open(URLContext *h, const char *filename, int flags) c->fd = fd; h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode); + if (c->loop) + h->is_streamed = 1; return 0; } @@ -137,6 +138,8 @@ static int64_t file_seek(URLContext *h, int64_t pos, int whence) FileContext *c = h->priv_data; int64_t ret; + if (h->is_streamed) + return AVERROR(EINVAL); if (whence == AVSEEK_SIZE) { struct stat st; ret = fstat(c->fd, &st); @@ -154,6 +157,18 @@ static int file_close(URLContext *h) return close(c->fd); } +static int file_read(URLContext *h, unsigned char *buf, int size) +{ + FileContext *c = h->priv_data; + int r = read(c->fd, buf, size); + if (r == 0 && c->loop && c->flags & AVIO_FLAG_READ) { + file_close(h); + file_open(h, c->fname, c->flags); + r = file_read(h, buf, size); + } + return (-1 == r)?AVERROR(errno):r; +} + URLProtocol ff_file_protocol = { .name = "file", .url_open = file_open, -- 1.7.10.4