Commit Graph

61 Commits

Author SHA1 Message Date
wm4 1301a90761 demux: add a demuxer thread
This adds a thread to the demuxer which reads packets asynchronously.
It will do so until a configurable minimum packet queue size is
reached. (See options.rst additions.)

For now, the thread is disabled by default. There are some corner cases
that have to be fixed, such as fixing cache behavior with webradios.

Note that most interaction with the demuxer is still blocking, so if
e.g. network dies, the player will still freeze. But this change will
make it possible to remove most causes for freezing.

Most of the new code in demux.c actually consists of weird caches to
compensate for thread-safety issues (with the previously single-threaded
design), or to avoid blocking by having to wait on the demuxer thread.

Most of the changes in the player are due to the fact that we must not
access the source stream directly. the demuxer thread already accesses
it, and the stream stuff is not thread-safe.

For timeline stuff (like ordered chapters), we enable the thread for the
current segment only. We also clear its packet queue on seek, so that
the remaining (unconsumed) readahead buffer doesn't waste memory.

Keep in mind that insane subtitles (such as ASS typesetting muxed into
mkv files) will practically disable the readahead, because the total
queue size is considered when checking whether the minimum queue size
was reached.
2014-07-16 23:25:56 +02:00
wm4 23a7257cca Revert "Remove DVD and Bluray support"
This reverts commit 4b93210e0c.

*shrug*
2014-07-15 01:49:02 +02:00
wm4 4b93210e0c Remove DVD and Bluray support
It never worked well. Just remux your DVD and BD images to mkv.
2014-07-14 14:34:14 +02:00
wm4 4981367021 cache, dvd, bluray: simplify stream time handling
We used a complicated and approximate method to cache the stream
timestamp, which is basically per-byte. (To reduce overhead, it was only
cached per 8KB-block, so it was approximate.)

Simplify this, and read/keep the timestamp only on discontinuities. This
is when demux_disc.c actually needs the timestamp.

Note that caching is currently disabled for dvdnav, but we still read
the timestamp only after some data is read. libdvdread behaves well, but
I don't know about libbluray, and the previous code also read the
timestamp only after reading data, so try to keep it safe.

Also drop the start_time offset. It wouldn't be correct anymore if used
with the cache, and the idea behind it wasn't very sane either (making
the player to offset the initial playback time to 0).
2014-07-07 19:09:37 +02:00
wm4 1d55547adf stream: remove now unused STREAM_CTRL_GET_START_TIME
demux_disc.c takes care of this now.
2014-07-06 19:02:33 +02:00
wm4 338004bcfc dvd, bluray, cdda: add demux_disc containing all related hacks
DVD and Bluray (and to some extent cdda) require awful hacks all over
the codebase to make them work. The main reason is that they act like
container, but are entirely implemented on the stream layer. The raw
mpeg data resulting from these streams must be "extended" with the
container-like metadata transported via STREAM_CTRLs. The result were
hacks all over demux.c and some higher-level parts.

Add a "disc" pseudo-demuxer, and move all these hacks and special-cases
to it.
2014-07-05 17:07:15 +02:00
wm4 7e209185f1 demux, stream: change metadata notification
(Again.)

This time, we simply make it event-based, as it should be. This is done
for both demuxer metadata and stream metadata.

For some ogg-over-icy streams, 2 updates are reported on stream start.
This is because libavformat reports an update right on start, while
including the same info in the "static" metadata. I don't know if that's
a bug or a feature.
2014-07-05 17:07:14 +02:00
wm4 7084e800be cache: clear DVD timestamps
When resizing the cache, the buffer for the DVD timestamps is
initialized with 0. This causes the player to always return playback
position 0 with any file format (not just DVD), and also makes all
relative seeks relative to position 0. Fix this by clearing the
timestamps explicitly.

Closes #899.

CC: @mpv-player/stable
2014-07-02 19:01:47 +02:00
wm4 ae1b1a68cb cache: avoid race condition between cache wakeup and idling
When the reader is out of data, it tries to wake up the cache thread to
get more data. In theory, there's a small race condition, which could
cause the cache to miss the wakeup and idle before reaction.

Most certainly didn't cause real issues, because even if this extremely
unlikely race condition happens, the cache won't idle for longer than
1 second (the hardcoded cache idle time).
2014-06-16 01:00:59 +02:00
wm4 598245a80f cache: print cache size only in verbose mode
Seems pretty useless in general, so this reduces output noise.
2014-06-12 00:55:14 +02:00
wm4 e82af029a9 stream/cache: handle failure of seeking underlying stream
This could for example happen when serving an incomplete file from http,
and the demuxer tries reading data from the end of the file when opening
it (e.g. with avi). Seeking past EOF fails with http, so the file could
never be opened, and the cache would get stuck trying to seek to the
position.

We can't really make the cache report seek failure directly (it would
suck for various reasons), so just make the cache report EOF if seeking
fails.
2014-06-05 00:19:50 +02:00
wm4 a4d487f5b2 stream: don't use end_pos
Stop using it in most places, and prefer STREAM_CTRL_GET_SIZE. The
advantage is that always the correct size will be used. There can be no
doubt anymore whether the end_pos value is outdated (as it happens often
with files that are being downloaded).

Some streams still use end_pos. They don't change size, and it's easier
to emulate STREAM_CTRL_GET_SIZE using end_pos, instead of adding a
STREAM_CTRL_GET_SIZE implementation to these streams.

Make sure int64_t is always used for STREAM_CTRL_GET_SIZE (it was
uint64_t before).

Remove the seek flags mess, and replace them with a seekable flag. Every
stream must set it consistently now, and an assertion in stream.c checks
this. Don't distinguish between streams that can only be forward or
backwards seeked, since we have no such stream types.
2014-05-24 16:17:51 +02:00
wm4 b37e3cc0ee cache: be silent if no initial fill is requested
Hides the "Cache fill:" message with default settings.
2014-05-22 13:07:18 +02:00
wm4 4664f8b3b7 cache: redo options and default settings
Some options change from percentages to number of kilobytes; there are
no cache options using percentages anymore.

Raise the default values. The cache is now 25000 kilobytes, although if
your connection is slow enough, the maximum is probably never reached.
(Although all the memory will still be used as seekback-cache.)

Remove the separate --audio-file-cache option, and use the cache default
settings for it.
2014-05-20 02:40:22 +02:00
wm4 f47a4fc3d9 threads: use mpv time for mpthread_cond_timedwait wrapper
Use the time as returned by mp_time_us() for mpthread_cond_timedwait(),
instead of calculating the struct timespec value based on a timeout.
This (probably) makes it easier to wait for a specific deadline.
2014-05-18 19:20:32 +02:00
wm4 e0cf983e53 stream: remove interrupt callback global variables
This used global variables for the asynchronous interrupt callback.

Pick the simple and dumb solution and stuff the callback into
mpv_global. Do this because interrupt checking should also work in the
connect phase, and currently stream creation equates connecting.
Ideally, this would be passed to the stream on creation instead, or
connecting would be separated from creation. But since I don't know yet
which is better, and since moving stream/demuxer into their own thread
is something that will happen later, go with the mpv_global solution.
2014-04-25 19:12:24 +02:00
wm4 4e3534f82c cache: remove redundant log prefix 2014-04-23 22:30:37 +02:00
wm4 cd10af4db6 threads: fix function name
Closer to the corresponding standard function pthread_cond_timedwait.
2014-04-23 21:16:52 +02:00
wm4 24f1878e95 stream_dvd, cache: hack seeking with --cache + dvd:// back into working
This was broken at some unknown point (even before the recent cache
changes). There are several problems:
- stream_dvd returning a random stream position, confusing the cache
  layer (cached data and stream data lost their 1:1 corrospondence by
  position)
- this also confused the mechanism added with commit a9671524, which
  basically triggered random seeking (although this was not the only
  problem)
- demux_lavf requesting seeks in the stream layer, which resulted in
  seeks in the cache or the real stream

Fix this by completely removing byte-based seeking from stream_dvd. This
already works fine for stream_dvdnav and stream_bluray. Now all these
streams do time-based seeks, and pretend to be infinite streams of data,
and the rest of the player simply doesn't care about the stream byte
positions.
2014-04-09 23:12:31 +02:00
wm4 d6086fa9ec cache: fix description of the offset field
This field sure is a bit strange. I hope the description is correct now.
2014-04-09 22:45:55 +02:00
wm4 5131fe13e1 cache: change a define to an enum
More consistent.
2014-04-09 22:36:01 +02:00
wm4 3836bfb1ad cache: fix checks/output on initialization
resize_cache() checks the size itself and clamps the size to the valid
range if necessary, so we don't need these checks. In fact, the checks
are different. Also, output the cache size after clamping, instead of
before.
2014-04-09 22:34:58 +02:00
wm4 e06d57b7f8 cache: simplify
Merge the cache_read function into cache_fill_buffer, since there's
not much reason to keep them separate. Also, simply call read_buffer()
to see if there's any readable data, instead of checking for the
condition manually.
2014-04-09 19:26:35 +02:00
wm4 5f65a5cfea cache: allow resizing at runtime
The only tricky part is keeping the cache contents, which is made simple
by allocating the new cache while still keeping the old cache around,
and then copying the old data.

To explain the "Don't use this when playing DVD or Bluray." comment: the
cache also associates timestamps to blocks of bytes, but throws away the
timestamps on seek. Thus you will experience strange behavior after
resizing the cache until the old cached region is exhausted.
2014-04-09 19:15:23 +02:00
wm4 6ac98c042f cache: minor simplification
The only difference is that the MP_DBG message is not printed anymore if
the current user read position is outside of the current cache range.

(In order to handle seek_limit==0 gracefully in the normal case of
linear reading, change the comparison from ">=" to ">".)
2014-04-09 19:07:50 +02:00
wm4 a967152498 cache: adjust stream position if necessary
Until now, this could never happen, because new data was simply always
appended to the end of the cache. But for making stream cache resizing
easier, doing it this way seems advantageous. It also makes it harder to
make the internal state inconsistent. (Before this change it could
happen that cache and stream position went out of sync if the read
position was adjusted "inappropriately".)
2014-04-09 19:06:21 +02:00
wm4 80392efaea cache: no short reads in read_buffer
Until now, cache_read() (which calls read_buffer()) could return short
reads. This was a simplification allowed by the stream interface. But
for cache resizing, it will be more practical to make read_buffer() do
a full read.
2014-04-09 19:05:41 +02:00
wm4 e7a5124b36 cache: move ringbuffer read into a separate function
No functional changes yet.
2014-04-09 19:03:23 +02:00
wm4 4c4c7bdeda cache: fix typo in comment 2014-04-09 19:03:17 +02:00
wm4 d6c4b35b0b cache: always update cached controls after running a stream control
Seems like a good idea. One possible bad effect would be slowing down
uncached controls, but they're already slow. The good thing is that
many controls make intrusive changes to the stream (at least controls
which do write accesses), so the cached parameters should be updated.
2014-04-09 19:01:32 +02:00
wm4 2c693a4732 stream: remove old chapter handling code
Stream-level chapters (like DVD etc.) did potentially not have
timestamps for each chapter, so STREAM_CTRL_SEEK_TO_CHAPTER and
STREAM_CTRL_GET_CURRENT_CHAPTER were needed to navigate chapters. We've
switched everything to use timestamps and that seems to work, so we can
simplify the code and remove this old mechanism.
2014-03-25 01:38:18 +01:00
xylosper 2f72eecd89 command: set 'media-title' property for bluray disc with meta-data 2014-03-13 14:36:20 +01:00
wm4 f30149e80e cache: cache DVD volume ID
Since this might be queried every frame or so, it's important not to
stall the cache by doing a synchronous stream_control().
2014-02-23 18:16:33 +01:00
wm4 eb17780740 cache: refuse to seek outside of cache boundaries
Note that this still happens in the stream level, so we can't have
nice highlevel behavior restricting seeking. Instead, if a seek leads
to the demuxer requesting data outside of the cached range, the seek
will simply fail. This might confuse the demuxer, and the resulting
behavior is not necessarily useful.

Note that this also doesn't try to skip data on a forward seek. This
would just freeze the stream with slow unseekable streams.

One nice thing is that stream.h has a separate function for merely
skipping data (separate from seeking forward), which is pretty useful
in this case: we want skipping of data to work, even if we reject
seeking forward by skipping data as too expensive. This probably is
or will be useful for demux_mkv.c.
2014-01-31 22:40:35 +01:00
wm4 7e1bc6be00 cache: remove debug code
This was accidentally committed.

<rcombs> `mp_sleep_us(100000);` <-- are you drunk?
2014-01-17 00:15:14 +01:00
wm4 68f46675bc cache: reduce message spam
Output only 1 message every 5 seconds at most.
2014-01-16 23:06:41 +01:00
wm4 0335011f11 stream: mp_msg conversions
We also drop some slave mode stuff from stream_vcd.
2013-12-21 21:43:16 +01:00
wm4 0112143fda Split mpvcore/ into common/, misc/, bstr/ 2013-12-17 02:39:45 +01:00
wm4 7dc7b900c6 Replace mp_tmsg, mp_dbg -> mp_msg, remove mp_gtext(), remove set_osd_tmsg
The tmsg stuff was for the internal gettext() based translation system,
which nobody ever attempted to use and thus was removed. mp_gtext() and
set_osd_tmsg() were also for this.

mp_dbg was once enabled in debug mode only, but since we have log level
for enabling debug messages, it seems utterly useless.
2013-12-16 20:41:08 +01:00
wm4 fc75506e8b cache: add a way to explicitly resume cache 2013-12-14 00:59:26 +01:00
wm4 1948131a2a cache: try harder on EOF
EOF is a special case. Normally, the reader will block until the cache
thread has new data. Obviously we don't want to do this on EOF, because
we'd potentially block forever. On the other hand, EOF will put the
cache thread into a waiting state, so if EOF recovers, this will happen
at a "later" point. This is bad if there is some kind of external event
that ends the EOF condition. In this case, a steram_read() call would
still return EOF. Make it so that the reader waits at least for one
iteration of the cache trying to rad a new block.

Also adjust some debug messages to not print file positions in hex.
2013-12-14 00:58:06 +01:00
wm4 0530447417 Add prelimimary (basic, possibly broken) dvdnav support
This readds a more or less completely new dvdnav implementation, though
it's based on the code from before commit 41fbcee. Note that this is
rather basic, and might be broken or not quite usable in many cases.

Most importantly, navigation highlights are not correctly implemented.
This would require changes in the FFmpeg dvdsub decoder (to apply a
different internal CLUT), so supporting it is not really possible right
now. And in fact, I don't think I ever want to support it, because it's
a very small gain for a lot of work. Instead, mpv will display fake
highlights, which are an approximate bounding box around the real
highlights.

Some things like mouse input or switching audio/subtitles stream using
the dvdnav VM are not supported.

Might be quite fragile on transitions: if dvdnav initiates a transition,
and doesn't give us enough mpeg data to initialize video playback, the
player will just quit.

This is added only because some users seem to want it. I don't intend to
make mpv a good DVD player, so the very basic minimum will have to do.
How about you just convert your DVD to proper video files?
2013-12-12 01:46:45 +01:00
wm4 b78d11d328 stream: split out pthread helper function
Also split the function itself into 3.
2013-11-17 16:42:57 +01:00
Stefano Pigozzi 406241005e core: move contents to mpvcore (2/2)
Followup commit. Fixes all the files references.
2013-08-06 22:52:31 +02:00
wm4 cb1ee41cec cache: fix time check for printing warning
This actually waited 2 seconds, because CACHE_WAIT_TIME happened to
be 0.5.
2013-07-20 02:16:54 +02:00
James Ross-Gowan ea7e54cb34 w32: silence some warnings 2013-07-13 14:43:54 +02:00
Stefano Pigozzi 846f46ec1d cache: fix compilation without posix timers
This is a regression caused by 854303a. This commit removed the include of
`sys/time.h` which was included in `cache.c` through a chain of recurvive
includes.
2013-07-08 20:04:07 +02:00
wm4 3f3ffd0de4 core: update metadata during playback, allow streams to export metadata
STREAM_CTRL_GET_METADATA will be used to poll for streamcast metadata.
Also add DEMUXER_CTRL_UPDATE_INFO, which could in theory be used by
demux_lavf.c. (Unfortunately, libavformat is too crappy to read metadata
mid-stream for mp3 or ogg, so we don't implement it.)
2013-07-02 12:19:16 +02:00
wm4 e00621ac46 cache: fix per-block metadata memory leak 2013-07-02 12:19:16 +02:00
wm4 536871d7e5 cache: cache number of chapters
Querying this caused the cache to block and wait. Some parts of the
frontend (like progress bar) call this very often, so cache performance
was ruined in these cases.

Also print a message in -v mode when the cache is blocked for a
STREAM_CTRL. This should make debugging similar issues easier.
2013-06-24 11:36:15 +02:00