mirror of
https://github.com/mpv-player/mpv
synced 2024-12-31 11:42:14 +00:00
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.
This commit is contained in:
parent
6aa6778ac4
commit
1cac7d1a65
@ -270,22 +270,19 @@ struct playlist *playlist_parse_file(const char *file, struct mpv_global *global
|
||||
struct mp_log *log = mp_log_new(NULL, global->log, "!playlist_parser");
|
||||
mp_verbose(log, "Parsing playlist file %s...\n", file);
|
||||
|
||||
struct playlist *ret = NULL;
|
||||
stream_t *stream = stream_open(file, global);
|
||||
if(!stream) {
|
||||
mp_err(log, "Error while opening playlist file %s\n", file);
|
||||
struct demuxer_params p = {.force_format = "playlist"};
|
||||
struct demuxer *d = demux_open_url(file, &p, NULL, global);
|
||||
if (!d) {
|
||||
talloc_free(log);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct demuxer_params p = {.force_format = "playlist"};
|
||||
struct demuxer *pl_demux = demux_open(stream, &p, global);
|
||||
if (pl_demux && pl_demux->playlist) {
|
||||
struct playlist *ret = NULL;
|
||||
if (d && d->playlist) {
|
||||
ret = talloc_zero(NULL, struct playlist);
|
||||
playlist_transfer_entries(ret, pl_demux->playlist);
|
||||
playlist_transfer_entries(ret, d->playlist);
|
||||
}
|
||||
free_demuxer(pl_demux);
|
||||
free_stream(stream);
|
||||
free_demuxer_and_stream(d);
|
||||
|
||||
if (ret) {
|
||||
mp_verbose(log, "Playlist successfully parsed\n");
|
||||
|
@ -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++)
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -209,7 +209,8 @@ int m_config_parse_mp_command_line(m_config_t *config, struct playlist *files,
|
||||
struct playlist *pl = playlist_parse_file(param0, global);
|
||||
talloc_free(param0);
|
||||
if (!pl) {
|
||||
MP_FATAL(config, "Error reading playlist '%.*s'", BSTR_P(p.param));
|
||||
MP_FATAL(config, "Error reading playlist '%.*s'\n",
|
||||
BSTR_P(p.param));
|
||||
goto err_out;
|
||||
}
|
||||
playlist_transfer_entries(files, pl);
|
||||
|
@ -684,11 +684,6 @@ struct track *mp_add_external_file(struct MPContext *mpctx, char *filename,
|
||||
if (strncmp(disp_filename, "memory://", 9) == 0)
|
||||
disp_filename = "memory://"; // avoid noise
|
||||
|
||||
struct stream *stream = stream_open(filename, mpctx->global);
|
||||
if (!stream)
|
||||
goto err_out;
|
||||
stream_enable_cache(&stream, &opts->stream_cache);
|
||||
|
||||
struct demuxer_params params = {
|
||||
.expect_subtitle = filter == STREAM_SUB,
|
||||
};
|
||||
@ -702,11 +697,10 @@ struct track *mp_add_external_file(struct MPContext *mpctx, char *filename,
|
||||
break;
|
||||
}
|
||||
|
||||
struct demuxer *demuxer = demux_open(stream, ¶ms, mpctx->global);
|
||||
if (!demuxer) {
|
||||
free_stream(stream);
|
||||
struct demuxer *demuxer =
|
||||
demux_open_url(filename, ¶ms, mpctx->playback_abort, mpctx->global);
|
||||
if (!demuxer)
|
||||
goto err_out;
|
||||
}
|
||||
|
||||
struct track *first = NULL;
|
||||
for (int n = 0; n < demuxer->num_streams; n++) {
|
||||
|
Loading…
Reference in New Issue
Block a user