From 720bcd79d0304dd82e607efa95d421f402c8a3dd Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 12 Aug 2020 17:23:13 +0200 Subject: command: add a way to access properties of a current track Requested. Should be good for simple use cases. "sub2" is technically inconsistent (since the option is called --secondary-sid), but fuck the consistent name. --- DOCS/man/input.rst | 20 ++++++++++++++++++++ player/command.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/DOCS/man/input.rst b/DOCS/man/input.rst index 8593dd75a2..6c589e7521 100644 --- a/DOCS/man/input.rst +++ b/DOCS/man/input.rst @@ -2780,6 +2780,26 @@ Property list "replaygain-album-peak" MPV_FORMAT_DOUBLE "replaygain-album-gain" MPV_FORMAT_DOUBLE +``current-tracks/...`` + This gives access to currently selected tracks. It redirects to the correct + entry in ``track-list``. + + The following sub-entries are defined: ``video``, ``audio``, ``sub``, + ``sub2`` + + For example, ``current-tracks/audio/lang`` returns the current audio track's + language field (the same value as ``track-list/N/lang``). + + A sub-entry is accessible only if a track of that type is actually selected. + Tracks selected via ``--lavfi-complex`` never appear under this property. + ``current-tracks`` and ``current-tracks/`` are currently not accessible, and + will not return anything. + + Scripts etc. should not use this. They should use ``track-list``, loop over + all tracks, and inspect the ``selected`` field to test whether a track is + selected (or compare the ``id`` field to the ``video`` / ``audio`` etc. + options). + ``chapter-list`` List of chapters, current entry marked. Currently, the raw property value is useless. diff --git a/player/command.c b/player/command.c index 7b3e194f78..aa57dfee8e 100644 --- a/player/command.c +++ b/player/command.c @@ -2025,6 +2025,53 @@ static int property_list_tracks(void *ctx, struct m_property *prop, get_track_entry, mpctx); } +static int property_current_tracks(void *ctx, struct m_property *prop, + int action, void *arg) +{ + MPContext *mpctx = ctx; + + if (action != M_PROPERTY_KEY_ACTION) + return M_PROPERTY_UNAVAILABLE; + + int type = -1; + int order = 0; + + struct m_property_action_arg *ka = arg; + bstr key; + char *rem; + m_property_split_path(ka->key, &key, &rem); + + if (bstr_equals0(key, "video")) { + type = STREAM_VIDEO; + } else if (bstr_equals0(key, "audio")) { + type = STREAM_AUDIO; + } else if (bstr_equals0(key, "sub")) { + type = STREAM_SUB; + } else if (bstr_equals0(key, "sub2")) { + type = STREAM_SUB; + order = 1; + } + + if (type < 0) + return M_PROPERTY_UNKNOWN; + + struct track *t = mpctx->current_track[order][type]; + if (!t) + return M_PROPERTY_UNAVAILABLE; + + int index = -1; + for (int n = 0; n < mpctx->num_tracks; n++) { + if (mpctx->tracks[n] == t) { + index = n; + break; + } + } + assert(index >= 0); + + char *name = mp_tprintf(80, "track-list/%d/%s", index, rem); + return mp_property_do(name, ka->action, ka->arg, ctx); +} + static int mp_property_hwdec_current(void *ctx, struct m_property *prop, int action, void *arg) { @@ -3445,6 +3492,7 @@ static const struct m_property mp_properties_base[] = { {"chapter-list", mp_property_list_chapters}, {"track-list", property_list_tracks}, + {"current-tracks", property_current_tracks}, {"edition-list", property_list_editions}, {"playlist", mp_property_playlist}, @@ -3584,7 +3632,7 @@ static const char *const *const mp_event_property_change[] = { E(MPV_EVENT_END_FILE, "*"), E(MPV_EVENT_FILE_LOADED, "*"), E(MP_EVENT_CHANGE_ALL, "*"), - E(MPV_EVENT_TRACKS_CHANGED, "track-list"), + E(MPV_EVENT_TRACKS_CHANGED, "track-list", "current-tracks"), E(MPV_EVENT_IDLE, "*"), E(MPV_EVENT_TICK, "time-pos", "audio-pts", "stream-pos", "avsync", "percent-pos", "time-remaining", "playtime-remaining", "playback-time", -- cgit v1.2.3