From 1cac7d1a659faffd1514a3269edf9fcae4d357c1 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 20 Feb 2015 21:56:55 +0100 Subject: demux: add a demux_open_url() function Often stream and a demuxer are opened at the same time. Provide a function for this and replace most of its uses. --- demux/demux.c | 20 ++++++++++++++++++ demux/demux.h | 7 +++++++ demux/demux_edl.c | 23 +++++---------------- demux/demux_mkv_timeline.c | 51 ++++++++++------------------------------------ 4 files changed, 43 insertions(+), 58 deletions(-) (limited to 'demux') diff --git a/demux/demux.c b/demux/demux.c index a08f633289..21898e3156 100644 --- a/demux/demux.c +++ b/demux/demux.c @@ -1039,6 +1039,26 @@ done: return demuxer; } +// Convenience function: open the stream, enable the cache (according to params +// and global opts.), open the demuxer. +// (use free_demuxer_and_stream() to free the underlying stream too) +struct demuxer *demux_open_url(const char *url, + struct demuxer_params *params, + struct mp_cancel *cancel, + struct mpv_global *global) +{ + struct MPOpts *opts = global->opts; + struct stream *s = stream_create(url, STREAM_READ, cancel, global); + if (!s) + return NULL; + if (!(params && params->disable_cache)) + stream_enable_cache(&s, &opts->stream_cache); + struct demuxer *d = demux_open(s, params, global); + if (!d) + free_stream(s); + return d; +} + static void flush_locked(demuxer_t *demuxer) { for (int n = 0; n < demuxer->num_streams; n++) diff --git a/demux/demux.h b/demux/demux.h index b76fb8fedc..b6aa4af517 100644 --- a/demux/demux.h +++ b/demux/demux.h @@ -172,6 +172,7 @@ struct demuxer_params { int matroska_wanted_segment; bool *matroska_was_valid; bool expect_subtitle; + bool disable_cache; // demux_open_url() only }; typedef struct demuxer { @@ -259,6 +260,12 @@ struct sh_stream *new_sh_stream(struct demuxer *demuxer, enum stream_type type); struct demuxer *demux_open(struct stream *stream, struct demuxer_params *params, struct mpv_global *global); +struct mp_cancel; +struct demuxer *demux_open_url(const char *url, + struct demuxer_params *params, + struct mp_cancel *cancel, + struct mpv_global *global); + void demux_start_thread(struct demuxer *demuxer); void demux_stop_thread(struct demuxer *demuxer); void demux_set_wakeup_cb(struct demuxer *demuxer, void (*cb)(void *ctx), void *ctx); diff --git a/demux/demux_edl.c b/demux/demux_edl.c index ba88e6a3d5..7986489397 100644 --- a/demux/demux_edl.c +++ b/demux/demux_edl.c @@ -134,22 +134,6 @@ error: return NULL; } -static struct demuxer *open_file(char *filename, struct timeline *tl) -{ - struct MPOpts *opts = tl->global->opts; - struct demuxer *d = NULL; - struct stream *s = stream_open(filename, tl->global); - if (s) { - stream_enable_cache(&s, &opts->stream_cache); - d = demux_open(s, NULL, tl->global); - } - if (!d) { - MP_ERR(tl, "EDL: Could not open source file '%s'.\n", filename); - free_stream(s); - } - return d; -} - static struct demuxer *open_source(struct timeline *tl, char *filename) { for (int n = 0; n < tl->num_sources; n++) { @@ -157,9 +141,12 @@ static struct demuxer *open_source(struct timeline *tl, char *filename) if (strcmp(d->stream->url, filename) == 0) return d; } - struct demuxer *d = open_file(filename, tl); - if (d) + struct demuxer *d = demux_open_url(filename, NULL, NULL, tl->global); + if (d) { MP_TARRAY_APPEND(tl, tl->sources, tl->num_sources, d); + } else { + MP_ERR(tl, "EDL: Could not open source file '%s'.\n", filename); + } return d; } diff --git a/demux/demux_mkv_timeline.c b/demux/demux_mkv_timeline.c index 64d4583979..71f5b16031 100644 --- a/demux/demux_mkv_timeline.c +++ b/demux/demux_mkv_timeline.c @@ -148,36 +148,6 @@ static char **find_files(const char *original_file) return results; } -static int enable_cache(struct mpv_global *global, struct stream **stream, - struct demuxer **demuxer, struct demuxer_params *params) -{ - struct MPOpts *opts = global->opts; - - if (!stream_wants_cache(*stream, &opts->stream_cache)) - return 0; - - char *filename = talloc_strdup(NULL, (*demuxer)->filename); - - free_demuxer_and_stream(*demuxer); - *stream = stream_open(filename, global); - if (!*stream) { - talloc_free(filename); - return -1; - } - - stream_enable_cache(stream, &opts->stream_cache); - - *demuxer = demux_open(*stream, params, global); - if (!*demuxer) { - talloc_free(filename); - free_stream(*stream); - return -1; - } - - talloc_free(filename); - return 1; -} - static bool has_source_request(struct matroska_segment_uid *uids, int num_sources, struct matroska_segment_uid *new_uid) @@ -202,16 +172,11 @@ static bool check_file_seg(struct tl_ctx *ctx, struct demuxer ***sources, .matroska_wanted_uids = *uids, .matroska_wanted_segment = segment, .matroska_was_valid = &was_valid, + .disable_cache = true, }; - struct stream *s = stream_open(filename, ctx->global); - if (!s) + struct demuxer *d = demux_open_url(filename, ¶ms, NULL, ctx->global); + if (!d) return false; - struct demuxer *d = demux_open(s, ¶ms, ctx->global); - - if (!d) { - free_stream(s); - return was_valid; - } struct matroska_data *m = &d->matroska_data; @@ -243,8 +208,14 @@ static bool check_file_seg(struct tl_ctx *ctx, struct demuxer ***sources, MP_TARRAY_APPEND(NULL, *sources, *num_sources, NULL); } - if (enable_cache(ctx->global, &s, &d, ¶ms) < 0) - continue; + if (stream_wants_cache(d->stream, &ctx->global->opts->stream_cache)) + { + free_demuxer_and_stream(d); + params.disable_cache = false; + d = demux_open_url(filename, ¶ms, NULL, ctx->global); + if (!d) + continue; + } (*sources)[i] = d; return true; -- cgit v1.2.3