diff options
author | wm4 <wm4@nowhere> | 2019-01-04 13:09:02 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2019-09-19 20:37:04 +0200 |
commit | 7fad173cfda06724a0af33091c26eec937d0c6cf (patch) | |
tree | 8ef65e15b3762f8fbb7b6387ba68975f6c7b3b52 /demux/demux_edl.c | |
parent | 1d0da7d950c3ce34d5c6540286a873930f447331 (diff) | |
download | mpv-7fad173cfda06724a0af33091c26eec937d0c6cf.tar.bz2 mpv-7fad173cfda06724a0af33091c26eec937d0c6cf.tar.xz |
demux, demux_edl: add extension for tracks sourced from separate streams
This commit adds an extension to mpv EDL, which basically allows you to
do the same as --audio-file, --external-file, etc. in a single EDL file.
This is a relatively quick & dirty implementation. The dirty part lies
in the fact that several shortcuts are taken. For example, struct
timeline now forms a singly linked list, which is really weird, but also
means the other timeline using demuxers (cue, mkv) don't need to be
touched. Also, memory management becomes even worse (weird object
ownership rules that are just fragile WTFs). There are some other
dubious small changes, mostly related to the weird representation of
separate streams.
demux_timeline.c contains the actual implementation of the separate
stream handling. For the most part, most things that used to be on the
top level are now in struct virtual_source, of which one for each
separate stream exists. This is basically like running multiple
demux_edl.c in parallel. Some changes could strictly speaking be split
into a separate commit, such as the stream_map type change.
Mostly untested. Seems to work for the intended purpose. Potential for
regressions for other timeline uses (like ordered chapters) is probably
low. One thing which could definitely break and which I didn't test is
the pseudo-DASH fragmented EDL code, of which ytdl can trigger various
forms in obscure situations. (Uh why don't we have a test suite.)
Background:
The intention is to use this for the ytdl wrapper. A certain streaming
site from a particularly brain damaged and plain evil Silicon Valley
company usually provides streams as separate audio and video streams.
The ytdl wrapper simply does use audio-add (i.e. adding it as external
track, like with --audio-file), which works mostly fine. Unfortunately,
mpv manages caching completely separately for external files. This has
the following potential problems:
1. Seek ranges are rendered incorrectly. They always use the "main"
stream, in this case the video stream. E.g. clicking into a cached range
on the OSC could trigger a low level seek if the audio stream is
actually not cached at the target position.
2. The stream cache bloats unnecessarily. Each stream may allocate the
full configured maximum cache size, which is not what the user intends
to do. Cached ranges are not pruned the same way, which creates disjoint
cache ranges, which only use memory and won't help with fast seeking or
playback.
3. mpv will try to aggressively read from both streams. This is done
from different threads, with no regard which stream is more important.
So it might happen that one stream starves the other one, especially if
they have different bitrates.
4. Every stream will use a separate thread, which is an unnecessary
waste of system resources.
In theory, the following solutions are available (this commit works
towards D):
A. Centrally manage reading and caching of all streams. A single thread
would do all I/O, and decide from which stream it should read next. As
long as the total TCP/socket buffering is not too high, this should be
effective to avoid starvation issues. This can also manage the cached
ranges better. It would also get rid of the quite useless additional
demuxer threads. This solution is conceptually simple, but requires
refactoring the entire demuxer middle layer.
B. Attempt to coordinate the demuxer threads. This would maintain a
shared cache and readahead state to solve the mentioned problems
explicitly. While this sounds simple and like an incremental change,
it's probably hard to implement, creates more messy special cases,
solution A. seems just a better and simpler variant of this. (On the
other hand, A. requires refactoring more code.)
C. Render an intersection of the seek ranges across all streams. This
fixes only problem 1.
D. Merge all streams in a dedicated wrapper demuxer. The general demuxer
layer remains unchanged, and reading from separate streams is handled as
special case. This effectively achieves the same as A. In particular,
caching is simply handled by the usual demuxer cache layer, which sees
the wrapper demuxer as a single stream of interleaved packets. One
implementation variant of this is to reuse the EDL infrastructure, which
this commit does.
All in all, solution A would be preferable, because it's cleaner and
works for all external streams in general.
Some previous commit tried to prepare for implementing solution A. This
could still happen. But it could take years until this is finally
seriously started and finished. In any case, this commit doesn't block
or complicate such attempts, which is also why it's the way to go.
It's worth mentioning that original mplayer handles external files by
creating a wrapper demuxer. This is like a less ideal mixture of A. and
D. (The similarity with A. is that extending the mplayer approach to be
fully dynamic and without certain disadvantages caused by the wrapper
would end up with A. anyway. The similarity with D. is that due to the
wrapper, no higher level code needs to be changed.)
Diffstat (limited to 'demux/demux_edl.c')
-rw-r--r-- | demux/demux_edl.c | 59 |
1 files changed, 38 insertions, 21 deletions
diff --git a/demux/demux_edl.c b/demux/demux_edl.c index c0225737e1..52b36ebfc9 100644 --- a/demux/demux_edl.c +++ b/demux/demux_edl.c @@ -49,6 +49,7 @@ struct tl_parts { char *init_fragment_url; struct tl_part *parts; int num_parts; + struct tl_parts *next; }; struct priv { @@ -79,6 +80,7 @@ static bool parse_time(bstr str, double *out_time) static struct tl_parts *parse_edl(bstr str) { struct tl_parts *tl = talloc_zero(NULL, struct tl_parts); + struct tl_parts *root = tl; while (str.len) { if (bstr_eatstart0(&str, "#")) { bstr_split_tok(str, "\n", &(bstr){0}, &str); @@ -138,13 +140,15 @@ static struct tl_parts *parse_edl(bstr str) break; } if (is_header) { - if (tl->num_parts) - goto error; // can't have header once an entry was defined bstr type = param_vals[0]; // value, because no "=" if (bstr_equals0(type, "mp4_dash")) { tl->dash = true; if (nparam > 1 && bstr_equals0(param_names[1], "init")) tl->init_fragment_url = bstrto0(tl, param_vals[1]); + } else if (bstr_equals0(type, "new_stream")) { + struct tl_parts *ntl = talloc_zero(tl, struct tl_parts); + tl->next = ntl; + tl = ntl; } continue; } @@ -152,11 +156,11 @@ static struct tl_parts *parse_edl(bstr str) goto error; MP_TARRAY_APPEND(tl, tl->parts, tl->num_parts, p); } - if (!tl->num_parts) + if (!root->num_parts) goto error; - return tl; + return root; error: - talloc_free(tl); + talloc_free(root); return NULL; } @@ -260,11 +264,12 @@ static void build_timeline(struct timeline *tl, struct tl_parts *parts) MP_WARN(tl, "Segment %d has unknown duration.\n", n); if (part->offset_set) MP_WARN(tl, "Offsets are ignored.\n"); - tl->demuxer->is_network = true; + if (tl->demuxer) + tl->demuxer->is_network = true; if (!tl->track_layout) { - source = open_source(tl, part->filename); - if (!source) + tl->track_layout = open_source(tl, part->filename); + if (!tl->track_layout) goto error; } } else { @@ -320,12 +325,8 @@ static void build_timeline(struct timeline *tl, struct tl_parts *parts) starttime += part->length; - if (source) { - tl->demuxer->is_network |= source->is_network; - - if (!tl->track_layout) - tl->track_layout = source; - } + if (source && !tl->track_layout) + tl->track_layout = source; } tl->parts[parts->num_parts] = (struct timeline_part) {.start = starttime}; tl->num_parts = parts->num_parts; @@ -352,16 +353,32 @@ static void build_mpv_edl_timeline(struct timeline *tl) { struct priv *p = tl->demuxer->priv; - struct tl_parts *parts = parse_edl(p->data); - if (!parts) { + struct timeline *root_tl = tl; + struct tl_parts *root = parse_edl(p->data); + if (!root) { MP_ERR(tl, "Error in EDL.\n"); return; } - MP_TARRAY_APPEND(tl, tl->sources, tl->num_sources, tl->demuxer); - if (!p->allow_any) - fix_filenames(parts, tl->demuxer->filename); - build_timeline(tl, parts); - talloc_free(parts); + + for (struct tl_parts *parts = root; parts; parts = parts->next) { + if (tl->demuxer) + MP_TARRAY_APPEND(tl, tl->sources, tl->num_sources, tl->demuxer); + if (!p->allow_any) + fix_filenames(parts, root_tl->demuxer->filename); + build_timeline(tl, parts); + + if (parts->next) { + struct timeline *ntl = talloc_zero(tl, struct timeline); + *ntl = (struct timeline) { + .global = tl->global, + .log = tl->log, + .cancel = tl->cancel, + }; + tl->next = ntl; + tl = ntl; + } + } + talloc_free(root); } static int try_open_file(struct demuxer *demuxer, enum demux_check check) |