Commit Graph

2695 Commits

Author SHA1 Message Date
wm4 c43fd88f59 scripting: fix racy crash if loading .run files fails
args->client was deallocated if the FDs were closed and nothing
referenced it (IPC socket codes detected the closed sockets and
asynchronously killed the mpv_handle in args->client). The problem was
that args->log depended on it, and was also destroyed.

Fix this by duplicating the mp_log.
2020-02-25 22:41:09 +01:00
wm4 0ec036bfd8 ta: remove two pointless wrappers 2020-02-23 19:48:25 +01:00
wm4 4839106a99 client API: fix race condition on client exit
The relatively recently added property update code has a race condition
when clients exit. It still tried to access mpv_handle during and after
it was destroyed.

The reason is that it unlocks the lock for the mpv_handle list (while
mpv_handle is locked), but nothing in mp_destroy_client() cares about
this case. The latter function locks mpv_handle only before/while it
makes sure all of its activity is coming to an end, such as asynchronous
requests, and property updates that are in progress. It did not include
the case when mp_client_send_property_changes() was still calling
send_client_property_changes() with mpv_handle locked.

Fix this by checking the mpv_handle.destroying field. This field can be
set only when mpv_handle is locked. While we're checking the lock, the
mpv_handle list is still locked, so at worst we might be at the point
before mp_destroy_client() locks the list again and finally destroys the
mpv_handle.

This is a hard to reproduce race condition which I spotted only once in
valgrind by chance, so the fix is unconfirmed.
2020-02-23 17:52:21 +01:00
sfan5 57ecfb4da2 ytdl_hook: fix URL extraction for manifests 2020-02-23 12:04:55 +01:00
wm4 c68b813678 ytdl_hook: prefer "format" over "format_note" field for track titles
Much more verbose, but on the other hand format_note is useless for the
alphabetic site with fragmented DASH streams.
2020-02-21 22:23:17 +01:00
wm4 dc82921012 ytdl_hook: use "format" as fallback for "format_note" for stream titles
"format_note" normally contains a semi-informative description of the
format. But some extractors, confusingly, have it in the "format" field.
2020-02-21 16:38:17 +01:00
wm4 2b9b77fcf3 ytdl_hook: fix audio codec with some extractors
E.g. soundcloud. While it still worked, not having the audio codec was
pretty annoying.
2020-02-21 16:27:56 +01:00
wm4 c1f7b0c535 ytdl_hook: fix Lua escapes
This was obviously nonsense. In Lua 5.1 this appeared to work correctly,
but it really turned "\." into "." (making the pattern accept any
character). The proper way is using "%" for escaping.
2020-02-21 16:23:43 +01:00
wm4 605e1fb766 ytdl_hook, edl: add fps, samplerate codec parameters
Well, didn't help much in the case I was interested it.
2020-02-21 14:48:23 +01:00
wm4 6337c1cc14 ytdl_hook: make codec mapping more declarative 2020-02-21 14:29:55 +01:00
wm4 04118eefb8 ytdl_hook: remove some old playlist redirection hack
Should not be needed anymore. In fact, it's probably dangerous.
2020-02-21 14:19:01 +01:00
wm4 4c32468241 ytdl_hook: enable default selection via --ytdl-format with all_formats
In all_formats mode, we've ignored what --ytdl-format did so far, since
we've converted the full format list, instead of just the formats
selected by youtube-dl.

But we can easily restore --ytdl-format behavior: just mark the selected
tracks as default tracks.
2020-02-21 14:18:35 +01:00
wm4 daa3c11e1b ytdl_hook: add length parameter to delay-loaded tracks only once
This was done for each media type, so muxed tracks had it twice, which
logged a dumb warning. Move it out of the per-media type loop.
2020-02-21 12:06:18 +01:00
wm4 3d225ad275 ytdl_hook: remove bitrate estimation from file size
I think this is unnecessary, and at worst done by youtube-dl itself
(didn't check).
2020-02-21 11:57:57 +01:00
wm4 76aaf74d30 ytdl_hook: use tbr for all tracks if vbr/abr not available
vbr and abr are the video and audio bitrates. Sometimes there is a weird
mix of any of them available, but in these cases, it's not good to fall
back to tbr if a specific track has no vbr/abr.

For example, the alphabetic site provides tbr only for the muxed
fallback stream, but using tbr would make the primitive mpv hls_bitrate
selection pick the compatibility stream for audio, because it appears to
have a higher bitrate than the other audio-only streams (because the
bitrate includes video). So we must not use tbr in this case.

On the other hand, formats coming from youtube-dl HLS master playlist
use will only have tbr set.

So as a heuristic, use the tbr only if it's the only bitrate available
in any track entry.
2020-02-21 11:57:21 +01:00
wm4 6d09a042e6 ytdl_hook: replace skip_muxed with force_all_formats option
I don't think the skip_muxed option was overlay useful. While it was
nice to filter out the low quality muxed versions (as it happens on the
alphabetic site, I suspect it's compatibility stuff), it's not really
necessary, and just makes for another tricky and rarely used
configuration option. (This was different before muxed tracks were also
delay-loaded, and including the muxed versions slowed down loading.)

Add the force_all_formats option instead, which handles the HLS case.
Set it to true because they are also delay-loaded now, and don't slow
down startup as much.
2020-02-21 11:50:58 +01:00
wm4 390e995b6a ytdl_hook: delay-load interleaved files
(Or if it's about HLS, just "muxed"/multiplexed streams.)

This only affects all_formats=yes,skip_muxed=no modes.
2020-02-21 00:20:22 +01:00
wm4 f321d8da99 ytdl_hook: signal duration in all_formats mode
If all streams were delay loaded, there was actually no duration present
at all in the EDL metadata. So the length was considered unknown by the
player frontend.
2020-02-20 14:55:14 +01:00
wm4 89203cc994 ytdl_hook: attempt to filter out muxed streams if all_formats is used
See manpage additions. We would have to extend delay_open to support
multiple sub-tracks (for audio and video), and we'd still don't know (?)
whether it might contain more than one stream each (thinking of HLS
master streams). And if it's a true interleaved file (such as a "normal"
mp4 file provided as fallback for more primitive players), we'd either
have to signal such "bundled" tracks, or waste bandwidth.

This restructures a lot. The if/else tree in add_single_video for format
selection was a bit annoying, so it's split into separate if blocks,
where it checks each time whether a URL was determined yet.
2020-02-20 14:54:09 +01:00
wm4 c2e62817f6 ytdl_hook: remove forgotten debug message
It even has a typo.
2020-02-20 14:40:01 +01:00
wm4 af9403577a ytdl_hook: use bitrate fields for bitrate metadata instead of file sizes
If available.
2020-02-20 12:34:06 +01:00
wm4 f5c1d71639 ytdl_hook: try to skip interleaved streams with all_formats
If a "format" has both audio and video codec set, it might contain both
audio and video. all_format assumes that each format is just a quality
variant containing a single track.

This seems to happen with sites that provide a HLS master URL.
youtube-dl tends to "butcher" it, and the result isn't very ideal. I
guess HLS "renditions" simply don't map well to youtube-dl's output
format and what mpv expects. Playing master HLS directly is also less
than ideal, because of libavformat's stupid probing.

Fix this by not using the delay-opening mechanism if it appears like we
detected such a case. Add a metadata override to set the track titles to
"muxed-N", to indicate that they form a single unit. (Mostly helpful for
testing.)
2020-02-20 12:25:45 +01:00
wm4 eaa10a25c7 ytdl_hook: iterate format list by array order
Shouldn't have any consequences. Probably makes the user-visible order
more stable.
2020-02-20 12:17:59 +01:00
wm4 4e11ac82aa player: change bitrate in track listing back to kilobits
Because the --hls-bitrate option takes the same unit.
2020-02-20 12:13:32 +01:00
wm4 e2ab6b7f35 scripting: add a way to run sub processes as "scripts"
This is just a more convenient way to start IPC client scripts per mpv
instance.

Does not work on Windows, although it could if the subprocess and IPC
parts are implemented (and I guess .exe/.bat suffixes are required).
Also untested whether it builds on Windows. A lot of other things are
untested too, so don't complain.
2020-02-19 22:18:15 +01:00
wm4 6d8b4ca742 ytdl_hook: add all_formats option
Pretty worthless I guess. I only tested one site (and 2 videos), it's
somewhat likely that it will break with other sites. Even if you leave
the option disabled (the default).

Slightly related to #3548. This will allows you to use the bitrate
stream selection mechanism, that was added for HLS, with normal videos.
2020-02-19 16:33:48 +01:00
wm4 43c13e5ea2 ytdl_hook: add a way to not pass --format to the command line
Might be helpful for... whatever.
2020-02-19 16:31:04 +01:00
wm4 b8f80b3854 player: print manifest per-stream bitrate information to terminal
Aka hls-bitrate. In turn, remove the demux_lavf.c hack, which made the
track title use this.
2020-02-19 16:26:22 +01:00
wm4 a4d95fe98b audio: remove outdated comment
Neither does it (directly) mess with filters, nor does it return a bool.

As noticed by a comment in #6333.
2020-02-19 14:50:54 +01:00
wm4 0b35b4c917 sub: make filter_sdh a "proper" filter, allow runtime changes
Until now, filter_sdh was simply a function that was called by sd_ass
directly (if enabled).

I want to add another filter, so it's time to turn this into a somewhat
more general subtitle filtering infrastructure.

I pondered whether to reuse the audio/video filtering stuff - but better
not. Also, since subtitles are horrible and tend to refuse proper
abstraction, it's still messed into sd_ass, instead of working on the
dec_sub.c level. Actually mpv used to have subtitle "filters" and even
made subtitle converters part of it, but it was fairly horrible, so
don't do that again.

In addition, make runtime changes possible. Since this was supposed to
be a quick hack, I just decided to put all subtitle filter options into
a separate option group (=> simpler change notification), to manually
push the change through the playloop (like it was sort of before for OSD
options), and to recreate the sub filter chain completely in every
change. Should be good enough.

One strangeness is that due to prefetching and such, most subtitle
packets (or those some time ahead) are actually done filtering when we
change, so the user still needs to manually seek to actually refresh
everything. And since subtitle data is usually cached in ASS_Track (for
other terrible but user-friendly reasons), we also must clear the
subtitle data, but of course only on seek, since otherwise all subtitles
would just disappear. What a fucking mess, but such is life. We could
trigger a "refresh seek" to make this more automatic, but I don't feel
like it currently.

This is slightly inefficient (lots of allocations and copying), but I
decided that it doesn't matter. Could matter slightly for crazy ASS
subtitles that render with thousands of events.

Not very well tested. Still seems to work, but I didn't have many test
cases.
2020-02-16 02:07:24 +01:00
wm4 29e15e6248 ytdl_hook.lua: delay load subtitles
Uses the infrastructure added in the previous commits. This is
admittedly a bit weird (constructing EDL URLs and such). But on the
other hand, adding this as "first class" mechanism directly to the
sub-add command or so would increase weirdness and unexpected behavior
in other places, or at least that's what I think.

To reduce confusion, this goes through the effort of mapping the webvtt
codec, so it's shown "properly" in the codec list. Without this it would
show "null", but still work. In particular, any non-webvtt codecs should
still work if libavcodec supports it.

Not sure if I should remove the --all-subs hack from the code. But I
guess it does no harm.
2020-02-15 18:30:42 +01:00
wm4 cc52a03401 audio: slightly simplify pull underrun message printing
A previous commit moved the underrun reporting to report_underruns(),
and called it from get_space(). One reason was that I worried about
printing a log message from a "realtime" callback, so I tried to move it
out of the way. (Though there's little justification other than a bad
feeling. While an older version of the pull code tried to avoid any
mutexes at all in the callback to accommodate "requirements" from APIs
like jackaudio, we gave up on that. Nobody has complained yet.)

Simplify this and move underrun reporting back to the callback. But
instead of printing the message from there, move the message into the
playloop. Change the message slightly, because ao->log is inaccessible,
and without the log prefix (e.g. "[ao/alsa]"), some context is missing.
2020-02-13 18:02:16 +01:00
wm4 5bf433b16f player: consider audio buffer if AO driver does not report underruns
AOs can report audio underruns, but only ao_alsa and ao_sdl (???)
currently do so. If the AO was marked as not reporting it, the cache
state was used to determine whether playback was interrupted due to slow
input.

This caused problems in some cases, such as video with very low video
frame rate: when a new frame is displayed, a new frame has to be
decoded, and since there it's so much further into the file (long frame
durations), the cache gets into an underrun state for a short moment,
even though both audio and video are playing fine. Enlarging the audio
buffer didn't help.

Fix this by making all AOs report underruns. If the AO driver does not
report underruns, fall back to using the buffer state.

pull.c behavior is slightly changed. Pull AOs are normally intended to
be used by pseudo-realtime audio APIs that fetch an audio buffer from
the API user via callback. I think it makes no sense to consider a
buffer underflow not an underrun in any situation, since we return
silence to the reader. (OK, maybe the reader could check the return
value? But let's not go there as long as there's no implementation.)
Remove the flag from ao_sdl.c, since it just worked via the generic
mechanism. Make the redundant underrun message verbose only.

push.c seems to log a redundant underflow message when resuming (because
somehow ao_play_data() is called when there's still no new data in the
buffer). But since ao_alsa does its own underrun reporting, and I only
use ao_alsa, I don't really care.

Also in all my tests, there seemed to be a rather high delay until the
underflow was logged (with audio only). I have no idea why this happened
and didn't try to debug this, but there's probably something wrong
somewhere.

This commit may cause random regressions.

See: #7440
2020-02-13 01:32:58 +01:00
wm4 e9fc53a10b player: add ab-loop-count option/property
As requested I guess. It behaves quite similar to the --loop* options.

Not quite happy with the idea that 1) the option is mutated on each
operation (but at least it's consistent with --loop* and doesn't require
more properties), and 2) the ab-loop command will do nothing once all
loop iterations are done. As a concession, the OSD shows something about
"disabled".

Fixes: #7360
2020-02-08 15:01:33 +01:00
Avi Halachmi (:avih) 756960bf3c js: require: directory-scripts: first look at <dir>/modules/
Also, add the function mp.get_script_directory() to let scripts know if
they're loaded as a directory and where.
2020-02-07 18:22:12 +02:00
Avi Halachmi (:avih) 68a1b47d4d js: require: don't use ~~/scripts/modules.js/
Directories inside ~~/scripts/ are now loaded as scripts, so don't use
it also for modules. Now there are no default module paths.

To compensate, we now try to run ~~/.init.js right after defaults.js,
so the user may extend the js init procedure via this script, e.g. for
adding default paths to mp.module_paths .
2020-02-07 18:20:33 +02:00
wm4 10a97f7cc3 osc: use cache state cache-duration field
Avoids an additional property access; see previous commit.
2020-02-07 16:16:18 +01:00
wm4 11d223228c command: add cache-duration to cache state property
Convenience; see following commit.
2020-02-07 16:15:55 +01:00
wm4 c48fd48f1e console: fix typo in previous commit
Now keypad enter actually works.
2020-02-07 14:05:04 +01:00
wm4 f659cfd6e3 console: manually map numeric keypad (KP*) bindings
While mpv normally uses the text a key produces (as opposed to physical
key mappings), this is different with the keypad. This is for the sake
of making it possible to distinguish between these keys and the normal
number keys on the left side of a full size keyboard.

There were complaints that the keypad doesn't interact with console.lua,
so manually map them. This ignores numlock (behaves as if it's always
on), and maps KP_DEC to "." (even though it's mapped to "," on some
keyboards). The /*-+ keys produce ASCII on mpv (at least with X11) as an
inexplicable inconsistency, so there are no mappings for these.

Fixes: #7431
2020-02-07 13:51:25 +01:00
wm4 c33bbc8694 screenshot: fix typo in comment
It was not meant to imply that screenshots are male. Female (and other)
screenshots are also welcome to this project.
2020-02-07 13:42:42 +01:00
wm4 a3bd8c3b5f player: make screenshot each-frame mode more accurate
Due to asynchronicity, we generally can't guarantee that a video frame
matches up with other events such as playback time change exactly (since
decoding, presentation, and property update all happen at different
times). This is a complaint in the referenced bug report, where
screenshot filenames in each-frame screenshot did not use the correct
timestamp, and instead was lagging behind by 1 frame.

But in this case, synchronicity was already pretty much forced with wait
calls. The only problem was that the playback time was updated at a
later time, which results in the observed 1 frame lag. Fix this by
moving the place where the screenshot is triggered in this mode.

Normal screenshots may still have the old problem. There is no effort
made to guarantee the timestamps absolutely line up, same as with the
OSD. (If you want a guarantee, you need to use a video filter, such as
libavfilter's drawtext. These will obviously use the proper timestamp,
instead of going through the somewhat asynchronous property etc. system
in the player frontend.)

Fixes: #7433
2020-02-07 13:32:21 +01:00
wm4 4bdf03779f lua: fix typo in comment 2020-02-06 23:02:23 +01:00
Oscar Manglaras c99cc13526 options.lua: avoid unnecessary on_update calls
The script was set up to only call on_update when the changelist
was non-empty. However, since the size operator does not operate
on dicts, it always returned 0 (which is truthy), thus on_update
would always be called when the script-opts property changed.
2020-02-06 22:57:53 +01:00
sfan5 a2eee88ccc stats: fix incorrect ass formatting on 3rd page when vo was switched
When switching between having a video visible or not,
stats.lua now picks up the required formatting changes
for the cache stats page to display correctly.
2020-02-06 22:57:53 +01:00
wm4 c58f9a6579 scripting: give proper name to scripts using a directory
The recently added feature to load scripts from a sub-directory. A
problem was that the script name was still derived from the actual
script source file that was loaded, which was always "main.lua" (or
similar), resulting in "main" as name.

Fix this by using the directory name.

One odd corner case is that you can do "--script=/path/foo////". As by
POSIX semantics, the extra "/" are ignored. However, the newly added
code strips only one separator, so mp_basename() returns "" (as it
should), and the empty name makes mp_new_client() fail in turn. I don't
really care about this, just don't do it. But mp_new_client() failure
was silent (since it normally never happens), so add at least an error
message for that, instead of being entirely silent.
2020-02-06 19:38:22 +01:00
wm4 7c5c057717 lua: use mp_path_is_absolute() for checking package paths
This makes it work with the shitty OS. Behavior on Linux should be the
same.
2020-02-06 14:15:31 +01:00
wm4 cce7062a8a lua: fix highly security relevant arbitrary code execution bug
It appears Lua's package paths try to load .lua files from the current
working directory. Not only that, but also shared libraries.

  WHAT THE FUCK IS WHOEVER IS RESPONSIBLE FOR THIS FUCKING DOING?

mpv isn't setting this package path; currently it's only extending it.
In any sane world, this wouldn't be a default. Most programs use
essentially random working directories and don't change it.

I cannot comprehend what bullshit about "convenience" or whatever made
them do something this broken and dangerous. Thousands of programs using
Lua out there will try to randomly load random code from random
directories.

In mpv's case, this is so security relevant, because mpv is normally
used from the command line, and you will most likely actually change
into your media directory or whatever with the shell, and play a file
from there. No, you don't want to load a (probably downloaded) shared
library from this directory if a script try to load a system lib with
the same name or so.

I'm not sure why LUA_PATH_DEFAULT in luaconf.h (both upstream and the
Debian version) put "./?.lua" at the end, but in any case, trying to
load a module that doesn't exist nicely lists all package paths in
order, and confirms it tries to load files from the working directory
first (anyone can try this). Even if it didn't, this would be
problematic at best.

Note that scripts are _not_ sandboxed. They're allowed to load system
libraries, which is also why we want to keep the non-idiotic parts of
the package paths.

Attempt to fix this by filtering out relative paths. This is a bit
fragile and not very great for something security related, but probably
the best we can do without having to make assumptions about the target
system file system layout. Also, someone else can fix this for Windows.

Also replace ":" with ";" (for the extra path). On a side note, this
extra path addition is just in this function out of laziness, since
I'd rather not have 2 functions with edit the package path.

mpv in default configuration (i.e. no external scripts) is probably not
affected. All builtin scripts only "require" preloaded modules, which,
in a stroke of genius by the Lua developers, are highest priority in the
load order. Otherwise, enjoy your semi-remote code execution bug.

Completely unrelated this, I'm open for scripting languages and
especially implementations which are all around better than Lua, and are
suited for low footprint embedding.
2020-02-06 11:59:24 +01:00
wm4 65cd9efa85 lua: add mp.get_script_directory() function
And add some clarifications/suggestions to the manpage.
2020-02-04 20:40:16 +01:00
wm4 6a83187b06 player: partially fix backward playback display of cached text subtitles
This simply didn't set the direction flag in most situations, which
meant the timestamps used in the subtitle renderer were nonsense,
leading to invisible subtitles.

This works only for text subtitles that are cached in the ASS_Track
state. Reading new subtitles is broken because the demuxer layer has
trouble returning subtitle packets backwards, and I think for rendering
bitmap subtitles, the pruning direction would have to be adjusted. (Not
sure if reversing the timestamps before the subtitle renderer backend is
even the right thing to do. At least for sd_ass.c, it seems to make
sense, because it caches subtitles with "normal" timestamps.)
2020-02-04 20:26:35 +01:00