mpv/demux
wm4 f9f2e1cc4e demux: hack for instant stream switching
This removes the delay when switching audio tracks in mkv or mp4 files.
Other formats are not enabled, because it's not clear whether the
demuxers fulfill the requirements listed in demux.h. (Many formats
definitely do not with libavformat.)

Background:

The demuxer packet cache buffers a certain amount of packets. This
includes only packets from selected streams. We discard packets from
other streams for various reasons. This introduces a problem: switching
to a different audio track introduces a delay. The delay is as big as
the demuxer packet cache buffer, because while the file was read ahead
to fill the packet buffer, the process of reading packets also discarded
all packets from the previously not selected audio stream. Once the
remaining packet buffer has been played, new audio packets are available
and you hear audio again.

We could probably just not discard packets from unselected streams. But
this would require additional memory and CPU resources, and also it's
hard to tell when packets from unused streams should be discarded (we
don't want to keep them forever; it'd be a memory leak).

We could also issue a player hr-seek to the current playback position,
which would solve the problem in 1 line of code or so. But this can be
rather slow.

So what we do in this commit instead is: we just seek back to the
position where our current packet buffer starts, and start demuxing from
this position again. This way we can get the "past" packets for the
newly selected stream. For streams which were already selected the
packets are simply discarded until the previous position is reached
again.

That latter part is the hard part. We really want to skip packets
exactly until the position where we left off previously, or we will skip
packets or feed packets to the decoder twice. If we assume that the
demuxer is deterministic (returns exactly the same packets after a seek
to a previous position), then we can try to check whether it's the same
packet as the one at the end of the packet buffer. If it is, we know
that the packet after it is where we left off last time.

Unfortunately, this is not very robust, and maybe it can't be made
robust. Currently we use the demux_packet.pos field as unique packet
ID - which works fine in some scenarios, but will break in arbitrary
ways if the basic requirement to the demuxer (as listed in the demux.h
additions) are broken. Thus, this is enabled only for the internal mkv
demuxer and the libavformat mp4 demuxer.

(libavformat mkv does not work, because the packet positions are not
unique. Probably could be fixed upstream, but it's not clear whether
it's a bug or a feature.)
2015-02-13 21:17:17 +01:00
..
codec_tags.c demux_mkv: support embedded coverart 2014-12-22 12:53:51 +01:00
codec_tags.h demux_mkv: support embedded coverart 2014-12-22 12:53:51 +01:00
demux.c demux: hack for instant stream switching 2015-02-13 21:17:17 +01:00
demux.h demux: hack for instant stream switching 2015-02-13 21:17:17 +01:00
demux_cue.c demux_cue: use stream_peek() 2014-11-16 18:13:41 +01:00
demux_disc.c demux_disc: fix CDDA seekability 2015-02-03 19:32:30 +01:00
demux_edl.c timeline: reject mplayer2 EDL files, change EDL header 2013-11-21 15:59:00 +01:00
demux_lavf.c demux: hack for instant stream switching 2015-02-13 21:17:17 +01:00
demux_libass.c demux_libass: change how external ASS subtitles are loaded 2014-03-15 22:17:51 +01:00
demux_mf.c demux_mf: move mf.c contents to demux_mf.c 2014-12-29 23:09:50 +01:00
demux_mkv.c demux: hack for instant stream switching 2015-02-13 21:17:17 +01:00
demux_playlist.c demux_playlist: unquote entries in pls playlists 2014-12-30 13:24:43 +01:00
demux_raw.c demux_raw: output smaller audio packets 2015-02-04 10:42:23 +01:00
demux_subreader.c command: export file format as property 2015-01-23 15:59:06 +01:00
demux_tv.c audio: decouple demux and audio decoder/filter sample formats 2014-09-24 22:55:50 +02:00
ebml.c demux_mkv: fix EBML parsing checks 2015-01-12 14:31:31 +01:00
ebml.h Move compat/ and bstr/ directory contents somewhere else 2014-08-29 12:31:52 +02:00
matroska.h demux_mkv: reduce log noise 2014-12-29 23:14:19 +01:00
packet.c demux_mkv: refactor packet parsing 2015-02-05 21:52:07 +01:00
packet.h demux_mkv: refactor packet parsing 2015-02-05 21:52:07 +01:00
stheader.h player: add stream selection by ffmpeg index 2014-10-21 13:19:20 +02:00