lavf: allow custom IO for all files

Some (de)muxers open additional files beyond the main IO context.
Currently, they call avio_open() directly, which prevents the caller
from using custom IO for such streams.

This commit adds callbacks to AVFormatContext that default to
avio_open2()/avio_close(), but can be overridden by the caller. All
muxers and demuxers using AVIO are switched to using those callbacks
instead of calling avio_open()/avio_close() directly.

(de)muxers that use the URLProtocol layer directly instead of AVIO
remain unconverted for now. This should be fixed in later commits.
This commit is contained in:
Anton Khirnov
2016-01-16 17:53:43 +01:00
parent 68395f8c99
commit 9f61abc811
15 changed files with 130 additions and 68 deletions

View File

@@ -140,8 +140,7 @@ static void hds_free(AVFormatContext *s)
for (i = 0; i < s->nb_streams; i++) {
OutputStream *os = &c->streams[i];
if (os->out)
avio_close(os->out);
os->out = NULL;
ff_format_io_close(s, &os->out);
if (os->ctx && os->ctx_inited)
av_write_trailer(os->ctx);
if (os->ctx && os->ctx->pb)
@@ -171,8 +170,7 @@ static int write_manifest(AVFormatContext *s, int final)
snprintf(filename, sizeof(filename), "%s/index.f4m", s->filename);
snprintf(temp_filename, sizeof(temp_filename), "%s/index.f4m.tmp", s->filename);
ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
&s->interrupt_callback, NULL);
ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL);
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
return ret;
@@ -190,7 +188,7 @@ static int write_manifest(AVFormatContext *s, int final)
int b64_size = AV_BASE64_SIZE(os->metadata_size);
char *base64 = av_malloc(b64_size);
if (!base64) {
avio_close(out);
ff_format_io_close(s, &out);
return AVERROR(ENOMEM);
}
av_base64_encode(base64, b64_size, os->metadata, os->metadata_size);
@@ -203,7 +201,7 @@ static int write_manifest(AVFormatContext *s, int final)
}
avio_printf(out, "</manifest>\n");
avio_flush(out);
avio_close(out);
ff_format_io_close(s, &out);
return ff_rename(temp_filename, filename);
}
@@ -240,8 +238,7 @@ static int write_abst(AVFormatContext *s, OutputStream *os, int final)
"%s/stream%d.abst", s->filename, index);
snprintf(temp_filename, sizeof(temp_filename),
"%s/stream%d.abst.tmp", s->filename, index);
ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
&s->interrupt_callback, NULL);
ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL);
if (ret < 0) {
av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
return ret;
@@ -284,15 +281,14 @@ static int write_abst(AVFormatContext *s, OutputStream *os, int final)
}
update_size(out, afrt_pos);
update_size(out, 0);
avio_close(out);
ff_format_io_close(s, &out);
return ff_rename(temp_filename, filename);
}
static int init_file(AVFormatContext *s, OutputStream *os, int64_t start_ts)
{
int ret, i;
ret = avio_open2(&os->out, os->temp_filename, AVIO_FLAG_WRITE,
&s->interrupt_callback, NULL);
ret = s->io_open(s, &os->out, os->temp_filename, AVIO_FLAG_WRITE, NULL);
if (ret < 0)
return ret;
avio_wb32(os->out, 0);
@@ -305,14 +301,13 @@ static int init_file(AVFormatContext *s, OutputStream *os, int64_t start_ts)
return 0;
}
static void close_file(OutputStream *os)
static void close_file(AVFormatContext *s, OutputStream *os)
{
int64_t pos = avio_tell(os->out);
avio_seek(os->out, 0, SEEK_SET);
avio_wb32(os->out, pos);
avio_flush(os->out);
avio_close(os->out);
os->out = NULL;
ff_format_io_close(s, &os->out);
}
static int hds_write_header(AVFormatContext *s)
@@ -476,7 +471,7 @@ static int hds_flush(AVFormatContext *s, OutputStream *os, int final,
avio_flush(os->ctx->pb);
os->packets_written = 0;
close_file(os);
close_file(s, os);
snprintf(target_filename, sizeof(target_filename),
"%s/stream%dSeg1-Frag%d", s->filename, index, os->fragment_index);