Skip to content

Commit 5c3d217

Browse files
committed
avformat/file: seek back to initial position for fd protocol
So user's fd can be passed to libavformat multiple times in sequence without changing the position. Signed-off-by: Zhao Zhili <[email protected]>
1 parent 287e22f commit 5c3d217

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

libavformat/file.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ typedef struct FileContext {
9898
#if HAVE_DIRENT_H
9999
DIR *dir;
100100
#endif
101+
int64_t initial_pos;
101102
} FileContext;
102103

103104
static const AVOption file_options[] = {
@@ -218,7 +219,12 @@ static int fd_dup(URLContext *h, int oldfd)
218219
static int file_close(URLContext *h)
219220
{
220221
FileContext *c = h->priv_data;
221-
int ret = close(c->fd);
222+
int ret;
223+
224+
if (c->initial_pos >= 0 && !h->is_streamed)
225+
lseek(c->fd, c->initial_pos, SEEK_SET);
226+
227+
ret = close(c->fd);
222228
return (ret == -1) ? AVERROR(errno) : 0;
223229
}
224230

@@ -286,6 +292,7 @@ static int file_open(URLContext *h, const char *filename, int flags)
286292

287293
av_strstart(filename, "file:", &filename);
288294

295+
c->initial_pos = -1;
289296
if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
290297
access = O_CREAT | O_RDWR;
291298
if (c->trunc)
@@ -494,6 +501,11 @@ static int fd_open(URLContext *h, const char *filename, int flags)
494501
if (c->fd == -1)
495502
return AVERROR(errno);
496503

504+
if (h->is_streamed)
505+
c->initial_pos = -1;
506+
else
507+
c->initial_pos = lseek(c->fd, 0, SEEK_CUR);
508+
497509
return 0;
498510
}
499511

0 commit comments

Comments
 (0)