Commit Graph

654 Commits

Author SHA1 Message Date
Guido Cella 41372c5e1f auto_profiles.lua: apply profiles when conditions are truthy
Instead of erroring when values returned by profile-cond expressions
aren't booleans, apply the relative profiles as long as the return
values are truthy. This allows shortening conditions like

profile-cond=path:match('foo') ~= nil

to

profile-cond=path:match('foo')
2023-04-05 18:32:18 +00:00
Christoph Heinrich b7ffe0d16e osc: skip rendering when osd_dimensions are 0
The idle logo could appear on the left side of the window for a split
second after starting.
That is because when osd dimensions can be reported as 0 at the very beginning.
Since the width gets calculated based on a fixed height and the aspect ratio,
which is 0, that results in a width of 0 until the next update.
2023-04-05 17:14:43 +00:00
Dudemanguy 6e4a76db08 auto_profiles: check for non-existent properties
Previously, it just silently didn't do anything which is not very
intuitive. Since the lua api returns an error string, check to see if
it matches the "property not found" case and print an error message.
Additionally, don't add the fake property to the internal
cached_properties list or try to observe it. This avoids redundant
evaluate calls which will never actually succeed. We do still mark it
under watched_properties however. This avoids having to call
mp.get_property_native multiple times.
2023-03-29 13:33:57 +00:00
Harri Nieminen 292a5868cb various: fix typos
Found by codespell
2023-03-28 19:29:44 +00:00
Christoph Heinrich 362256edbc ytdl_hook: only log error when no fallback url available
An error indicates that something doesn't work, but as long as a
safe url is available, playback is still expected to work.

Thus reduce logging level of MP4 DASH without fragments message and
add a new error message for when there is no safe url available either.

Also adds a missing space.
2023-03-03 23:02:13 +00:00
Christoph Heinrich 94c189dae7 ytdl_hook: init fragment requires other fragments
With dash the first fragment was always considered an init fragment if
there wasn't a duration. However that only makes sense when there are
also other fragments, so check if there are other fragments in addition
to the lack of a duration.
2023-03-03 23:02:13 +00:00
Dudemanguy c5d1230517 {console,ytdl_hook}: use the platform property
The console.lua check is still kind of dumb since we check an
environment variable to distinguish between wayland and x11, but
otherwise it should be better in theory.
2023-02-27 17:13:21 +00:00
Christoph Heinrich 8ea7aa5471 ytdl_hook: don't overwrite force-media-title
ytdl_hook always set force-media-title, making users unable to force
a media-title via options.

To prevent that, check if force-media-title is already set to avoid
overwriting it.

A better solution would be to use tags like is already done for some
metadata, however that doesn't work when `all_formats=yes` is used.
See cbb8f534b0
A comment was added to hint at why it isn't done via tags.

ref.
https://github.com/mpv-player/mpv/pull/10453#issuecomment-1445277496
2023-02-27 17:07:38 +00:00
Guido Cella 42fd6f5f6f ytdl_hook.lua: make exec return a single value
This refactors exec to only return the result of the subprocess command,
and makes the rest of run_ytdl_hook use the fields of this result,
because specifying all those return values multiple times is unwieldy
now that exec is called several times, and this is easier to read
anyway.

I removed the line
err = string.format("%s returned '%d'", err, es)
altogether instead of updating the es variable, because there was no
chance of it being executed since it would only happen when
result.killed_by_us is true, but run_ytdl_hook returns early when it is.
2023-02-27 17:04:42 +00:00
rcombs d941564d48 console: observe osc margins from user-data instead of shared-script-properties 2023-02-26 17:54:09 -06:00
rcombs 894f0c28eb osc: set values in user-data in addition to shared-script-properties 2023-02-26 17:54:09 -06:00
dyphire 070287aebb stats.lua: display HDR peak in nits
The value of `sig-peak` is relative to the SDR peak. This is not
a problem when used inside the player, but the `HDR peak` in stats
should display human-readable information.
So change to return the actual nits value of HDR.
Closed https://github.com/mpv-player/mpv/issues/10127
2023-02-02 14:22:40 +00:00
Avi Halachmi (:avih) 86093fcae7 lua/js: remove user-data helpers
This reverts:
  3fb4140c lua/defaults: add user_data helpers
  68a20e7a javascript/defaults: add user_data helpers
  00510379 lua/js: fix user_data_del util function

As well as the lua/js parts of:
  3ec2a098 docs: document new user-data property

user-data and its sub-properties can be set/get/observed/deleted
via the standard properties interface, so there's no need for
additional helpers specific to user-data, which only added maintenance
burden.
2023-01-29 01:52:31 +02:00
rcombs 0051037957 lua/js: fix user_data_del util function 2023-01-28 15:21:15 -06:00
rcombs 3fb4140c38 lua/defaults: add user_data helpers 2023-01-28 14:37:21 -06:00
Dudemanguy bca516bd2c osc: don't spam window-controls bindings on every render
Not sure when this actually started happening, but it's probably been
like this for years. Currently, the logic for the window-controls works
by simply checking if the osc is visible and then either enabling or
disabling the associated keybindings. The problem is that this can just
constantly spam mp.enable_keybindings/disable_key_bindings on every
single render call if the user disables the border at any point in time.
This does a lot of pointless work and also results in the logs being
spammed with lines like "disable-section". Clearly, this should just
work like the code for checking the input keybindings just above it.
Keep track of an internal state variable and check when it doesn't match
the osc visibility. In that case, we can then either enable or disable
the key bindings and just update the variable.
2022-11-29 16:40:36 +00:00
Christoph Heinrich 445a3561d3 console: add history deduplication
Deduplicate history like the fish shell. So for example
entering "cmd 1" then "cmd 2" then "cmd 3" then "cmd 1"
would result in a history of
[cmd 2][cmd 3][cmd 1]
instead of
[cmd 1][cmd 2][cmd 3][cmd 1]

Adds a function `history_add` to replace directly adding to history.
Adds an option `history_dedup` to activate the deduplication.
Defaults to on.
2022-11-03 13:30:58 +01:00
Christoph Heinrich 1a633a6cbc ytdl_hook: reverse order of tracks
Only matters when configuring ytdl_hook with `all_formats=yes`.

So far the tracks were ordered from worst to best quality.

Web players with quality selection always show the highest quality
option at the top. Since tracks are usually listed with the first
track at the top, that should also be the highest quality one.

yt-dlp/youtube-dl sorts it's formats from worst to best.
Iterate in reverse to get best track first.
2022-11-01 09:54:33 -04:00
Christoph Heinrich fd91776207 console: respect the top margin shared script property
Console already respected the bottom margin to not overlap with the
bottom bar from the OSC, but it would still overlap with the window
decorations from the OSC.

Now everything is clipped above the top margin and no superfluous lines
are drawn.
2022-10-28 13:04:32 +02:00
Christoph Heinrich cc65b3892d osc: scale rendered aspect ratio with window aspect ratio
The logo stays centered better and it doesn't distort anymore due to a
recent change in libass https://github.com/libass/libass/pull/645
2022-10-06 14:34:56 -04:00
Christoph Heinrich ac7f3913d3 ytdl_hook: use subtitle `name` as title if available
So far the `ext` was always used as the title,
but `name` is more appropriate when it's available.
2022-09-23 19:18:49 +00:00
Christoph Heinrich e302c9b7dd ytdl_hook: simplify exclude option parsing
The parsing used to be unnecessarily complicated,
now it's easier to read and understand.
2022-09-14 13:29:11 +02:00
Christoph Heinrich 08dd30fde8 ytdl_hook: improve track detection
Every format that was not detected as a video format was added to the
audio tracks. This resulted in e.g. YouTube storyboards from ending up
in the list of audio tracks.

Now formats that are already known to be neither video formats nor audio
formats, will also not end up in any track list.

Formats where it is unknown if they are video or audio get added to
tracks if `force_all_formats` is used, otherwise only
formats that are known to contain video or audio become video or audio
tracks respectively.

https://github.com/yt-dlp/yt-dlp/issues/4373#issuecomment-1186637357
2022-08-17 20:04:00 +00:00
Christoph Heinrich 1ef53f094b ytdl_hook: consistant behavior for single format
One would expect that e.g.
`--script-opts=ytdl_hook-all_formats=no --ytdl-format=bestaudio` and
`--script-opts=ytdl_hook-all_formats=yes --ytdl-format=bestaudio`
to play the exact same tracks without manual intervention.
This already worked when two formats were requested.
For a single format with `all_formats=yes` it would also play a track
that was not requested when available. This was inconsistant with the
behavior of `all_formats=no` (default), which would not play a second
track when only a single one was requested.
This combined with #10395 now plays the exact same tracks with
`all_formats=yes` as without, even when only one format is requested.
2022-08-14 23:31:51 +00:00
Christoph Heinrich 813dfe1924 ytdl_hook: fix default track for single format
Tracks are marked as default tracks based on what yt-dlp/youtube-dl
returns in the field `requested_formats`. The problem is that this field
only exists when there is more then one requested format.
So `ytdl-format=bestvideo+bestaudio` would have that field,
but `ytdl-format=bestaudio` would not,
leading to no tracks being marked as default tracks.

The requested formats can also be found under `requested_downloads`,
which exists regardless of the number of requested formats.
However when there is more then one requested format,
`requested_downloads` doesn't contain those formats directly and instead
has a field `requested_formats` that is identical to the other
`requested_formats`. Therefore use `requested_downloads` as a fallback
for when `requested_formats` doesn't exist.
2022-08-12 14:31:59 +00:00
Christoph Heinrich 284fecc0bd ytdl_hook: fix playlist index extraction
The example given in #3024 would not play the correct video when
combined with `--ytdl-raw-options=yes-playlist=`.
Allowing `youtube:tab` as extractor and correcting the id check fixes
that.
2022-07-26 21:16:23 +00:00
Ryan Hendrickson ad5a1ac873 osc.lua: add osc-tcspace script option
Generally, the hard-coded sizes used for the OSC elements are
comfortable regardless of the font used, but the timecode fields have
relatively many characters, and so are affected to a greater degree by
fonts with a wider or narrower average character width than expected.

This allow users to adjust the space reserved for the timecode fields to
compensate.
2022-07-19 23:19:45 +03:00
Ryan Hendrickson ff25a8c65a osc.lua: add osc-unicodeminus script option
This option enables the use of the Unicode U+2212 Minus Sign character
when displaying the time remaining, instead of "-" (U+002D
Hyphen-Minus).
2022-07-19 23:19:45 +03:00
Christoph Heinrich 431473310f ytdl_hook: always set HTTP headers
In some cases HTTP headers were not set, leading to some sites not
working despite being supported by yt-dlp/youtube-dl.
2022-07-18 01:17:00 +00:00
CogentRedTester 1ffdb9128d lua: command_native_async: make the callback optional
The documentation states that the callback is optional, but it actually
was not.

Now it's optional, as docuented.
2022-06-22 14:44:52 +03:00
CogentRedTester 099ae86717 lua: command_native_async: always callback a-sync
Previously if the raw command_native_async returned an error then the
callback function was run directly. This meant that script writers
potentially had to account for both synchronous and asynchronous logic
in the callback, which can happen even with seemingly 'safe' commands
if the mpv event queue is full.

This was at odds with the Javascript implementation of
the function, which always runs the callback asynchronously.

Now the mp.add_timeout function is used to run the callback
asynchronously on error, replicating the Javascript implementation.

This provides consistency for developers in how the callback is handled
in Lua, and increases consistency between the Lua and Javascript APIs.
2022-06-22 14:44:12 +03:00
Dudemanguy ec236f7a99 osc.lua: add idlescreen and osc-idlescreen
This is mainly for other user scripts that may conflict with the osc
logo in some way. Although it is possible to listen for
shared-script-properties, this has many edge cases that could easily pop
up. A user could want other OSC things to happen at the same time (say
osc-message). They just don't want the logo. The idlescreen option
disables all idlescreen related things (including the santa hat) if it
is set to "no". A new script message (osc-idlescreen) is also added so
users can easily toggle the value (passing "cycle" or just explictly
setting "yes" or "no"). Some more discussion on this is found in the
below github issues.
https://github.com/mpv-player/mpv/issues/10201
https://github.com/CogentRedTester/mpv-file-browser/issues/55
2022-06-04 14:48:32 +00:00
CogentRedTester b4c73ed105 osc.lua: fix crash when calling osc-tracklist while idle
If the player is started with --idle  and the osc-tracklist script-message
is called then the tracks_osc table will be nil,
which caused the OSC to crash due to attempting to index a nil value.

This appears to be because the osc_tracklist variable is both
initialised and updated by (ultimately) the render() function, which
is not run when the player is idling.

Rather than mess with the existing OSC logic it's easier to either
add this single check, or alternatively to initialise the the tracks_osc
table somewhere before this.
2022-05-19 13:54:31 +00:00
Guido Cella fe9e074752 various: remove trailing whitespace 2022-05-14 14:51:34 +00:00
Guido Cella ebaf6a6cfa console.lua: don't render a 2nd cursor on the OSC
09ea3a424f moved the console above the OSC when it is visible so they
don't overlap, but only changed the first ass:pos() call and forgot to
update the second one, which redraws the cursor on top of the text, so
when both the OSC and the console are visible, a second cursor is drawn
on the OSC. You have to type past where the buttons are to notice it.

This commit synchronizes the position of the 2 ASS events.
2022-05-01 21:48:20 +10:00
Cœur bb5b4b1ba6 various: fix typos 2022-04-25 09:07:18 -04:00
Guido Cella 4d12dfb75a options.lua: remove unused function
val2str isn't used anywhere. Its only use was removed in dd3e52fe68,
when it was still part of osc.lua.
2022-04-08 19:20:49 +03:00
Avi Halachmi (:avih) b15b3f6711 ytdl_hook.lua: consider any subprocess status != 0 as error
Previously only status<0 was considered as error, but status>0 is
also an error (the process exit code). Change to status != 0.

This likely makes little to no difference in practice, because if
stdout is empty or can't be parsed as JSON then it's considered
an error anyway, but still, this is more correct.

Also, on error, add the complete subprocess result to the verbose log.
2022-03-03 15:01:40 +02:00
George Brooke 1a3e85ec33 ytdl_hook: fix url_is_safe to match URL protocols properly
Some youtube_dl extractors retrieve URLs which contain other URLs
inside of them, for example Funimation, like this:
https://example.com/video?parameter=https://example.net/something

The url_is_safe function uses a pattern to match the protocol at the
start of the URL. Before this commit, this pattern was not compliant
with the URL spec (see the definition of "A URL-scheme string"):
https://url.spec.whatwg.org/#url-writing
Therefore it would match any characters, including "://", until the
last occurence of "://" in the string. Thus the above URL would match
https://example.com/video?parameter=https
which is not in safe_protos so the video will not play.

Now the protocol can only start with a letter and only contain
alphanumerics, "." "+" or "-" as the spec says, so it will only match
the first protocol in the URL ("https" in the above example.)
Previously the URL also had to contain "//" after the ":". Data URLs
do not contain "//": https://datatracker.ietf.org/doc/html/rfc2397
so now the pattern does not look for "//", only ":".
2022-03-02 20:59:33 +02:00
Avi Halachmi (:avih) 57f42cee84 stats.lua: page 2 - frame timing: use fixed display order
Page 2 displays the frame timing info for each type (currently "Fresh"
and "Redraw"), but since they're unordered keys of a map, their
iteration order and hence their order on screen could (and did) change
depending on $things, which was annoying.

haasn thinks none of them should be considered more important (and
therefore maybe worthy of being displayed on top), so we go with
lexicographic order, which currently means that "Fresh" is first.

Fixes #9901
2022-02-23 02:20:49 +02:00
Avi Halachmi (:avih) 37927b65f7 stats.lua: graphs: fix bad rendering due to division by 0
This fixes two potential divisions by 0 at generate_graph(...):
- If v_avg is (given and) 0.
- if v_max is 0.

The former doesn't seem to happen in practice because v_avg is only
used at one generate_gpah call, where it's apparently not 0.

The latter triggers if all the graph values are 0 (hence v_max is
also 0).

The implication of these divisions by 0 is an invalid y-value which
ends up at the ASS coordinates string for the graph inner content.

On linux the value ends as "nan" (luajit) or "-nan" (lua 5.1/5.2), and
on Windows it's "nan" (luajit) or "-1.#IND00" (lua 5.1/5.2), maybe due
to msvcrt's snprintf.

All these strings are wrong as ASS numbers, but due to luck in how
libass parses the coordinates, "nan" and "-nan" result in empty graph
(which looks OK for "all values are 0"), while "-1.#IND00" is parsed
as -1, which breaks the graph rendering (affects other graphs too).

One example of "all values are 0" is at page 0 (internal performance
graphs) on Windows - because the cpu metrics don't work.
So this fixes at least page 0 on Windows with lua 5.1/5.2.

While at it, move the scale calculations to one place, which now
avoids division by 0, instead of duplicating this calculation.

In the future, we can consider improving the generate_graph API:
- It needs one peak value, but takes 3 (v_max, v_avg, scale) which
  are meshed into one final value.
- v_avg is only used in 1 of 6 call sites, but at the middle of the
  arguments, so all other call sites need to pass explicit "nil".
- "scale" is arbitrary and used to leave some space at the top of the
  graph. 5 places use 0.8, one uses 0.9. Could probably be unified.
2022-02-21 16:05:02 +02:00
Avi Halachmi (:avih) 0197729949 osc.lua: seekbar hover: speed-up chapter access
This speeds up chapter name access while hovering the seekbar:

- Fetch the chapter-list property using observation rather than on
  every render while hovering the bar. Property access, especially
  with potentially lots of values (chapters), can be expensive-ish,
  and can involve locking, so the less per-render the better.

- Sort the list once (on change) to simplify the iteration logic when
  searching the chapter which matches the mouse position. It's still
  O(N) (no binary search currently), but with less work.

The cached sorted list is stored at the state. While there are other
places which access this property and could use this cache, for now we
don't touch them, because they're not performance-critical (on init
for the chapter markers, on ch_{prev,next} button click for OSD).

Caching properties using observation instead of using mp.get_property
can indeed speedup other places too, but it should be part of a system
rather than implemented per-property. Maybe some day.
2022-02-06 11:02:39 +02:00
CogentRedTester 712ef65e2a auto_profiles.lua: don't warn if profile-restore=default
The code assumed that the value of the profile-restore field is
"default" for such profiles, but the C implementation is such
that the field is intentionally omitted in such case.

This resulted in incorrectly trying to restore such profiles. However,
there are no stored values with profile-restore=default, so it only
resulted in a harmless warning, but otherwise behaved correctly (i.e.
nothing is restored).

Fix the condition by taking into account that missing field means
"default", so that it doesn't try to restore in such case, and
avoid the warning.
2022-01-30 15:13:10 +02:00
sfan5 dfcd561ba9 lua: remove mp.suspend, resume and resume_all
These functions were deprecated in v0.21.0 and no-ops
since v0.23.0. The matching client API functions were
removed in the previous commit.
2021-12-15 12:29:10 +01:00
Funami580 448fe0226b osc: fix cache displaying 60s in some cases
It was caused by string.format rounding e.g. 59.9 to 60.
2021-11-27 21:37:24 +02:00
Avi Halachmi (:avih) b8926dd484 osc: add options: chapters/playlist OSD, hover chapter format
Previously OSD was always displayed on {ch,pl}_{next,prev} left-click,
and seekbar-hover-chapter was always enabled and with fixed format.

Now it can be controlled with:
- chapters_osd, playlist_osd: yes/no (only affects left-click).
- chapter_fmt: lua string.format template, or "no" to disable.

Fixes #4675
2021-11-25 21:20:48 +02:00
Umar Javed 923c0f0370 ytdl_hook.lua: improve check for sub language before inserting all-subs
youtube-dl and yt-dlp both support --sub-langs and --srt-lang in
addition to --sub-lang for defining languages of subtitles. This hook
only checked for sub-lang in --ytdl-raw-options and inserted --all-subs
in its absence.
2021-11-15 15:08:22 +01:00
Guido Cella 87c9eefb29 console.lua: define remaining emacs keybindings 2021-11-01 23:42:16 +11:00
Avi Halachmi (:avih) ca6108baf4 osc.lua: avoid infinite ticks loop on idle
Before this commit, animation-end was handled at render(), however,
it's not called on idle, which resulted in state.anitype ~= nil, with
nothing to reset it during idle, which caused in an infinite tick()
loop (starts on first mouse move).

Now tick resets the animation on idle, and also, as a safety measure,
if we're past 1s after the animation deadline.

The safety measure is because the osc states are complex, and it's
easier to detect a "we really shouldn't be animating now" at tick()
itself rather than detecting the exact states where animation should
be reset. Generally, the safety mmeasure is not needed.
2021-10-03 19:52:58 +03:00
Avi Halachmi (:avih) bc6dab6d92 osc.lua: unify animation reset function (no-op) 2021-10-03 19:52:58 +03:00
Guido Cella cc4ada655a ytdl_hook.lua: search for yt-dlp by default
Because youtube-dl is inactive and the yt-dlp fork is becoming more
popular, make mpv use yt-dlp without any extra configuration.

yt-dlp is ordered before youtube-dl because it's more obscure, so users
who have yt-dlp installed are more likely to want to use it rather than
youtube-dl.

Fixes #9208.
2021-09-25 13:57:36 +02:00
Stefan de Konink 0c9e1e34fd stats.lua: typo lavfi-complex
Fix typo lavi-complex to lavfi-complex.
2021-08-15 12:27:40 +03:00
Avi Halachmi (:avih) 8a597a484b lua: read_options: quote values at error messages
This makes it easier to understand the error in cases of incorrect
syntax or formatting at .conf files. (js already has this quoting).

Fixes #9105
2021-08-10 11:00:18 +03:00
Guido Cella 7eb34d2376 stats.lua: page 4 (keys): group current-window-scale under window
This groups "set current-window-scale ..." under the "window" group
instead of "current" in the list of keybindings.
2021-08-06 10:29:28 +03:00
Avi Halachmi (:avih) 416668d3c8 stats.lua: page 4 (keys): better alignment of non-ascii keys
Previously we assumed the key-name string occupies strlen(name) cells,
now we count codepoints instead.

This improves alignment of non-english key names. Still not perfect
because we don't know if the key name is single or double width, but
wcwidth not available to scripts, notoriously unreliable (depends on
locale, correct and updated tables, etc), and also not always
available (Windows).

Still, better than nothing, and we err by at most one cell - vs up to
three before this commit (4 bytes keyname codepoint).

In the future we could do the alignment using libass tags, however,
this both complicates the ass-output generation, and also not available
when we output for the terminal, so for now only count codepoints.

Also, if the key name was in a right-to-left language, then previously
the name/command were swapped visually. Now we inject a left-to-right
marker before the name to ensure direction. This works also when
harfbuzz is disabled for libass (--sub-ass-shaper=simple).
2021-07-31 11:00:07 +03:00
Avi Halachmi (:avih) 8b1930fbd6 stats.lua: page 4 (keys): detect single-quotes 2021-07-30 09:52:34 +03:00
Avi Halachmi (:avih) 955736b7b7 stats.lua: fix ass-escape while persistent_overlay=yes
mpv has two methods to display output from text input:
- show-text (scripting: mp.osd_message) has ass disabled by default
  (escaped), and the property osd-ass-cc can control escaping.
- osd-overlay (scripting: mp.set_osd_ass or mp.create_osd_overlay)
  has ass enabled (unescaped), and osd-ass-cc is NOT supported.

By default, stats.lua uses mp.osd_message which does support escaping.
That's persistent_overlay=no.

When using persistent_overlay=yes then mp.set_osd_ass is used.

Due to this, the no_ASS(..) function - which is supposed to escape
ass, simply returned its input unmodified when persistent_overlay
is enabled.

This is not a new issue, and the filters on page 1 use no_ASS() to no
avail in persistent mode, however, this content (filter name and value
strings) rarely actually needs escaping, and users didn't notice.

However, the new page 4 (keys) does break visibly when no_ASS doesn't
work, because it tries to escape arbitrary key-names and command
strings, and at the very least the key '{' is bound by default, which
is displayed incorrectly if escaping doesn't work.

Fix this by rolling our own escaping when using mp.set_osd_ass,
similar to how the mpv code does it for mp.osd_message (substrings
replacements).

However, this means that the set_ASS(..) function can no longer
behave correctly because escaping requires going through the whole
content string rather than only inserting a marker.

Luckily, other than at no_ASS, set_ASS was only used at one place
(text_style), which is only called from two places:
- generate_graph() only needs to restore styles - not to enable ass.
- add_header() is only used at the begining of page output, and
  uses set_ASS to enable ass initially when using mp.osd_message.

So remove the set_ASS function, and instead enable ass directly at
print_page using osd-ass-cc when mp.osd_message is used.

Fixes #9022
2021-07-25 15:08:44 +03:00
Guido Cella ccb87ad637 stats.lua: remove script-opts for the main keys (i/I)
Unlike the page switching/scrolling keys which are bound at runtime
and therefore we need to know which (configured) keys to bind, the
main keys (i/I by default) are static and can be bound via input.conf.
And indeed, the builtin bindings at etc/input.conf have them already.
2021-07-21 13:55:58 +03:00
Avi Halachmi (:avih) 70c9d8d5d7 stats.lua: page 4 (keys): fix "excluding stats keys" - attempt 2
Page 4 bindings listing wants to exclude the interactive keys of the
stats script itself - so that they don't hide the normal keys which
are bound when stats is not interactive.

It did so by testing that the bound command includes stats/__key
which is how it usually looks when a script binds keys.

However, if a script uses a name for the binding - like stats.lua does
for the interative keys (because it later removes them by name when
interactive mode ends), then the command has stats/name rather than
stats/__key...

To fix this, we change the names of the forced interactive bindings
to start with __forced_, and that's now also what the page-4 listing
excludes.
2021-07-20 01:00:03 +03:00
Avi Halachmi (:avih) 1e1b5a6e14 Revert "stats.lua: page 4 (keys): fix "excluding stats keys""
This reverts commit 0f1654811bda0dfdd337734ec33c59b67522234a.

It was an incorrect fix, because the key names do get used - to remove
the forced bindings once stats exits the interactive mode.

And without names - the interactive keys remained active even after
stats exited interactive mode.
2021-07-20 01:00:03 +03:00
Avi Halachmi (:avih) fab25ac004 stats.lua: page 4 (keys): fix "excluding stats keys"
The page 4 keybinding list tries to skip the interactive bindings of
stats itself (because they would hide the normal bindings of these
keys when stats is not visible), and to do that it excludes commands
containing "script-binding stats/__key" - which is how script-added
bindings usually look like.

However, keys which are added with a "name" bind stats/name rather
than stats/__key... - and that's what stats.lua did till now with its
interactive force-bindings.

Now the interactive forced bindings are added without a name, and so
they do end up using the automatic stats/__key... and get excluded.
2021-07-20 00:13:35 +03:00
Avi Halachmi (:avih) 2335ee5514 stats.lua: page 4 (keys): support help-like terminal printout
While --input-test is useful, and so is page 4 of stats, until now
there was no way to simply print the list in a help-like fashion.

This commit adds such printout, invoked by the script opt
stats-bindlist=yes, which uses the existing page 4 code. This prints
the list on startup and quits immediately - like any help page.

It's awkward to invoke compared to other help pages, and it does
require the stats page to be enabled (it is by default), however
it is a script-generated output, and currently there's no other
method to print a help page generated by a script.

The printout itself is performed using lua's io.write. While reliable,
it's not the standard way for mpv to print to the terminal.

Other possible printout methods are mp.msg.info - which also prints
"[stats]" prefix on each line (ugly), or forcing term-osd and setting
an osd-message which mpv will then print at the terminal - however
that's printed to stderr, and could also be subject to timing concerns
since we quit right afterwards.

In the future we can consider changing/integrating the invocation so
that mpv itself could print a help page generated by a script, thus
solving both the awkward invocation and printout-method issues.
2021-07-19 22:06:50 +03:00
Avi Halachmi (:avih) 9fb200b641 stats.lua: add page 4: active key-bindings list
This is a scrollable page which also works nicely with the terminal
OSD. Typically there are more than 100 bound keys.

It groups the binding using fuzzy property/command/script name after
simple analysis of the command string, and then further sorts the
binding in each group according to the "complexity" of the key itself
(plain keys -> keys with modifiers, alphabetically, etc).

The alignment/grouping style is heavily inspired by @medhefgo's #8924
but otherwise it's an independent implementation.
2021-07-19 22:06:50 +03:00
Avi Halachmi (:avih) 59c10274b4 stats.lua: scroll: allow throttling page-rebuild (no-op)
Typically the current page is rebuilt and rendered once per second,
howeve, scrolling can invoke the rebuild function very frequently,
even at a rate which is too fast for the rebuild function to keep
up. This can result in high CPU usage and input lag.

This commit adds an argument to the page-rebuild function, letting it
know whether or not it's called following a scroll keypress, thus
allowing it to cache intermediate data so that it doesn't have to
re-calculate the whole page from scratch.

This additional argument is unused currently, but it could be useful
for the internal performance page - which can be relatively heavy.
2021-07-19 22:06:50 +03:00
Avi Halachmi (:avih) 50280197e2 stats.lua: move internal performance from page 4 to page 0
This allows adding more pages without the internal performance page
getting stuck at the middle of the list.
2021-07-19 22:06:50 +03:00
Avi Halachmi (:avih) d828652f24 lua: fix timers comment (no-op)
process_timers() doesn't return an absolute time. It returned delta>0
or nil before f73814b1 , and since f73814b1 it can also return 0.
2021-07-13 13:11:34 +03:00
Avi Halachmi (:avih) d2dd4cacb8 osc: expose osc-visibility via shared-script-properties
osc-visibility can already be changed at runtime via script-message
or other means, but until now there was no way to tell what the
current state is.

Now shared-script-properties/osc-visibility reflects this state.
It's output-only by the osc - changing it does not affect the osc.

Useful if a script wants to change osc-visibility temporarily, and
later restore to its original state.

There's no way to coordinate if more than one script wants to change
it, but that would be a hard problem to solve anyway, even if the
osc itself tried to coordinate such requests from different sources.
2021-07-06 16:12:36 +03:00
Dudemanguy 76a53f9de3 stats.lua: add scaled resolution
Calculate the actual scaled size of the video from osd-dimensions and
display it on the stats page.
2021-06-25 17:54:36 +00:00
Avi Halachmi (:avih) 5b40db385a lua: idle observers: ensure timers are up-to-date
This fixes two issues, both of which resulted from the timers-wait
period not being re-calculated after idle-observers were executed:

- If timers were added from an idle observer then they could fire long
  after they were due (only when/if the next mpv event arrives).

- Idle observers don't execute in zero time, and the wait period for
  the next timer was implicitly extended by the idle observers
  execution time (because it was calculated before the idle observers).

This commit ensures that if idle-observers were executed, then the
timers wait period is re-calculated, which solves both issues.
2021-06-23 23:47:08 +03:00
Avi Halachmi (:avih) f73814b180 lua: timers: don't block forever with slow callbacks
Previously, process_timers() kept going as long as there were due
timers, which could be for extended periods of time or even forever
if there were slow timer callbacks with either periodic timers or if
timers were added repeatedly.

This prevented dequeuing mpv events, and subsequently, among others,
prevented mpv from quitting until process_timers() completed.

For instance, this caused process_timers() to never return:

  function render() <longer than 1/60 s on a slow system> end
  mp.add_periodic_timer(1/60, render)

Similarly, it never returned if a timer callback always added a new
one-shot which was already due by the time the callback completed.

This commit ensures that process_timers() only executes callbacks which
were due when it started, so that timers which are added (or repeated)
during process_timers() will wait for the next iteration - after mpv
events are dequeued.

This has no performance impact under normal conditions (when callbacks
complete before the next timer is due).

Additionally, previously idle-observers were executed unconditionally
after the timers because indeed there was nothing due when (if...)
process_timers() completed. However, now process_timers() can return
even if there are due timers, so skip idle-observers on such case.
2021-06-23 23:47:08 +03:00
Avi Halachmi (:avih) 76b1ac57a4 osc: seekbar hover/drag: display target chapter at the title
Fixes #8925
2021-06-22 18:12:29 +03:00
Avi Halachmi (:avih) 48f3a26746 osc: ensure tick after any mouse-event
Previously tick() was ensured unconditionally only after mouse-move,
but there are other mouse-events which need re-rendering (tick), like
mouse-down (icons may get grayed-out) or mouse-up (icons may change).

For instance: mpv --pause --myclip.mkv

then move the mouse over the volume/mute OSC button, then - without
moving the mouse - press and release the left mouse button.

The osc was not re-rendered because it's paused and the mouse didn't
move, so the volume icon didn't get grayed-out when held down, and the
icon doesn't change on mouse-up (to reflect the new mute state).

Now both these changes are rendered correctly.
2021-06-20 17:37:47 +03:00
Avi Halachmi (:avih) 89efe820a8 osc: don't initialize while mouse-down over an element
This is not a new issue, however, until the last commit - 96b246d
init probably didn't happen much (or at all) between mouse-down and
mouse-up, but after this commit, if there are chapters in a live-stream
then osc_init() is used to re-render the markers at the adjusted
position - which breaks the OSC buttons functionality if init happened
between mouse-down and mouse-up.
2021-06-20 17:37:47 +03:00
Sagnac 96b246d928 osc: update chapter marker positions when duration changes
Commit 6abb7e3 updates the markers when the chapters change, but it
doesn't update their relative position at the bar when the duration
changes.

This means that adding chapters to a live stream would result in
corresponding chapter markers which were static while the duration
changed and thus their positions became incorrect over time until the
OSC was reinitialized.

This is fixed by observing the duration property if chapters are present
and reinitializing the OSC when the duration changes.

The live_markers user option, which determines whether the duration
property is observed when there are chapters, has been added in order to
allow disabling this behaviour as calling request_init() frequently
might have some impact on low-end systems.

The impact of request_init() on render() was measured to increase from
1-1.5 ms to 2-3 ms on a 2010 MacBook Air, while the impact was neglible
on a 2016 Surface Book (increasing only to an average of 1.4 ms from
1.3 ms for n=1500 render cycles).

The live_markers option is enabled by default.
2021-06-16 02:04:28 +03:00
Retusthese fd8c673cfc ytdl_hook: improve handling of json parsing errors
This moves the JSON parsing above the main youtube-dl error-handling
block and integrates parsing errors into that block. Now, if a parsing
error occurs, youtube-dl's stderr will be logged as it is with other
errors. This also catches errors that cause youtube-dl to output
"null", which would previously be mishandled as a parsing error and
crash ytdl_hook when it attempted to concatenate the error string from
parse_json.
2021-05-29 19:54:03 +03:00
Retusthese 6abb7e385f osc: re-initialize when chapter list changes
When the OSC initializes, it checks whether the current video has
chapters, and if it does not, it disables its chapter functionality
(chapter buttons are grayed out and unusable, chapter indicators don't
show on the seek bar). If another script changed the chapter list
after the video has loaded, those changes would be ignored by the OSC
until some other event causes it to re-initialize, because it did not
observe the chapter list property. This is fixed by adding
observation of chapter-list alongside the other properties that
trigger re-initialization.
2021-05-27 09:12:38 +03:00
Your Name cbb8f534b0 ytdl_hook: expose some JSON fields as tags
Shows uploader, channel, description fields. This works only if the
media media is constructed as EDL (for youtube it usually does this),
and if the all_formats option is not set to true (does anyone even use
it?). The former usually happens because youtube serves audio and video
separately, though it will not for live HLS/DASH. The latter uses
delayed media opening, which breaks the global_tags mechanism (see
previous commit).
2021-05-11 22:21:46 +02:00
Avi Halachmi (:avih) 02fbdf8aaf scripting (lua/js): utils.getpid: make wrapper of pid property
We now have at least 3 scripting APIs which are trivial wrappers
around properties: mp.get_mouse_pos, utils.getcwd, utils.getpid.

After some discussion on IRC it was decided that it's easier for us to
maintain them as trivial wrappers than to deprecate them and inflict
pain on users and script authors, so currently no plan to deprecate.
2021-05-01 16:07:05 +03:00
ossifrage 0d384592c5 osc: reset margins when using boxvideo with showfullscreen/showwindowed
This fixes a bug where using boxvideo with showfullscreen=no or
showwindowed=no resulted in the margins not resetting when the OSC
wasn't visible.

For example, using:

script-opts=osc-visibility=always,osc-boxvideo=yes,osc-showfullscreen=no

and then going fullscreen would make the OSC disappear but the video
margins would remain. This is because boxvideo was missing a dependence
on the showfullscreen and showwindowed user options.

This is fixed by adding the corresponding conditions to update_margins()
and setting state.marginsREQ on fullscreen changes. update_margins() is
called on tick() if there's a margins update pending, which guarantees
the boxvideo margins are reset appropriately.
2021-04-27 14:12:05 +03:00
ossifrage c1568a3a6c osc: display immediately when visibility changes from never to always
Clearing state.osd.data with an empty string at render_wipe() fixes an
issue where changing the OSC visibility from "never" directly to
"always" didn't immediately update the display when the player was
paused. This could be verified by starting the player with
`--script-opts=osc-visibility=always --pause` and then running
`script-message osc-visibility never` followed by
`script-message osc-visibility always`.

Removing the overlay without changing the contents meant the overlay
wouldn't update and display when enabled again until the fields changed
in some way (e.g. seeking, mousing over the OSC area, etc.). Clearing
state.osd.data before removal of the OSC makes sure set_osd doesn't
return on re-enable and instead displays the OSC immediately as the data
is now different.

render_wipe() is now also used when the OSC needs to be cleared at
tick() as using set_osd to clear it with an empty string did not call
state.osd:remove() which can allow cleanups related to bitmap memory
allocations etc.
2021-04-25 19:24:46 +03:00
Chris Varenhorst 5824d9fff8 stats.lua: include a filter's @label when displaying filters on page 1 2021-03-15 23:00:42 +02:00
rnhmjoj 45e6804478 ytdl_hook: fix crash on missing track bitrate
Some tracks happen to lack bitrate information (ie. no tbr value).
In that case, just ignore the track while computing the max bitrate.

For an example, this is a stream in which all audio tracks
have no bitrate: https://www.raiplay.it/dirette/rai1
2021-03-08 14:01:34 +02:00
Guido Cella 7ca14d646c console: use wl-paste on Wayland 2020-12-14 22:43:34 +00:00
Philip Sequeira df805cfc84 auto_profiles: fix compile_cond on lua 5.1
5.1's load() doesn't accept strings; loadstring must be used instead.

In 5.2, loadstring is deprecated and setfenv is gone.
2020-12-08 17:29:19 +00:00
Avi Halachmi (:avih) 0d5055fe93 lua/js: mp.get_mouse_pos: use the mouse-pos property
mp.get_mouse_pos() is undocumented and is no longer required - the
property can be used officially by any client now, however, osc.lua
uses it, and also some user scripts learnt to rely on it, so we keep
it - as a trivial wrapper around the new mouse-pos property.
2020-11-16 20:29:58 +02:00
Guido Cella e49404cba9 console: let type set the cursor position
This allows keybindings such as:

a script-message-to console type "seek :0 absolute" 6
% script-message-to console type "seek  absolute-percent" 6

The cursor position 0 isn't allowed because it has the weird effect of
filling the console with the text twice, leaving the cursor in the
middle.
Negative positions would put the cursor n characters before the end, and
positions greater than the text's length at the end. They seem to work
at first, but the console breaks when you move the cursor, so they
aren't allowed.
It seems that float values don't cause issues, but I'm using the
argument's floor anyway to be safe. Using >= 1 instead of > 0 ignores
values like 0.5.
2020-10-27 17:10:50 +00:00
Ricardo Constantino ad1ecd4350
ytdl_hook: if ytdl not found in config dirs, use ytdl_path as is 2020-10-27 15:42:39 +00:00
Ricardo Constantino 93f84b514a
ytdl_hook: support alternative youtube-dl path
Allows using a youtube-dl not in PATH or a compatible fork of
youtube-dl.
2020-10-27 15:42:39 +00:00
sfan5 cbbdb3fae4 stats: display hw pixel format too 2020-10-16 17:48:05 +02:00
wm4 5dcfe32ff2 ytdl_hook: sort subtitle list by language
The subtitle list is returned in randomized order, because a table (i.e.
JSON object) is used. To make the order stable across repeated
invocations, sort it by language.
2020-08-12 20:16:13 +02:00
wm4 4c72202eb1 auto_profiles: unapply conditional profiles if declared
Uses the mechanism introduced in the previous commit. The hope was to
make auto-profiles easier to use, and to get rid of the need for
manually created inverse profiles. Not sure if the end result is useful.
2020-08-07 19:41:56 +02:00
wm4 53ee1ae417 auto_profiles: register hooks for more synchronous profile application
The property observation mechanism is fairly asynchronous to the player
core, and Lua scripts also are (they run in a separate thread). This may
sometimes lead to profiles being applied when it's too load.

For example, you may want to change network options depending on the
input URL - but most of these options would have to be set before the
HTTP access is made. But it could happen that the profile, and thus the
option, was applied at an slightly but arbitrary later time.

This is generally not fixable. But for the most important use-cases,
such as applying changes before media opening or playback
initialization, we can use some of the defined hooks.

Hooks make it synchronous again, by allowing API users (such as scripts)
to block the core because it continues with loading.

For this we simply don't continue a given hook, until we receive an idle
event, and have applied all changes. The idle event is in general used
to wait for property change notifications to settle down. Some of this
relies on the subtle ways guarantees are (maybe) given. See commit
ba70b150fb for the messy details. I'm not quite sure whether it
actually works, because I can't be bothered to read and understand my
bullshit from half a year ago. Should provide at least some improvement,
though.
2020-08-05 23:28:24 +02:00
wm4 d0ab562b1f lua: make hook processing more flexible
This can now opt to not continue a hook after the hook callback returns.
This makes it easier for scripts, and may make it unnecessary to run
reentrant event loops etc. for scripts that want to wait before
continuing while still running the event loop.
2020-08-05 23:19:41 +02:00
wm4 13d354e46d auto_profiles: add this script
This is taken from a somewhat older proof-of-concept script. The basic
idea, and most of the implementation, is still the same. The way the
profiles are actually defined changed.

I still feel bad about this being a Lua script, and running user
expressions as Lua code in a vaguely defined environment, but I guess as
far as balance of effort/maintenance/results goes, this is fine.

It's a bit bloated (the Lua scripting state is at least 150KB or so in
total), so in order to enable this by default, I decided it should
unload itself by default if no auto-profiles are used. (And currently,
it does not actually rescan the profile list if a new config file is
loaded some time later, so the script would do nothing anyway if no auto
profiles were defined.)

This still requires defining inverse profiles for "unapplying" a
profile. Also this is still somewhat racy. Both will probably be
alleviated to some degree in the future.
2020-08-05 22:37:47 +02:00
Eva 359261c50c stats: fix crash when aspect ratio is unavailable
When switching between files it's possible that r["aspect"] returns nil, resulting in a crash.
2020-08-03 00:50:57 +03:00
Derek Guenther b238897614 ytdl_hook: fix typo in unexpected error message 2020-08-01 15:02:39 +03:00
sfan5 b67c7a23b8 stats: fix single invocation keybindings 2020-07-21 19:34:41 +02:00
wm4 1b03970d79 lua: windows got what users crave
It's got '\r's.

Fixes: #7733
2020-05-27 15:01:39 +02:00
Julian c7b94c03f1 stats: UP/DOWN scrolling on page 2 (frame stats)
Code contributed by @avih with only minor modifications to comments by
me.
Fixes #7727.
2020-05-17 20:11:50 +02:00
wm4 152b0e2a8c osc: fix hovering timestamp sticking around when moving mouse away
The OSC calls this "tooltip" (and although a general mechanism, there's
only one instance using it). One particular problem was that with the
default OSC layout, moving the mouse down and out of the window, the
tooltip stuck around, because the returned mouse position was the last
pixel row in the window, which still overlaps with the seek bar.

Instead of introducing mouse_in_window, you could check last_mouse_X for
nil, but I think this is clearer.

This returns (-1, -1) to the caller if the mouse is outside. Kind of
random, but works.
2020-05-16 13:45:16 +02:00