mpv: fix race condition in uosc/ao-volume monitoring

This commit is contained in:
2024-04-05 22:41:16 +00:00
parent 907933612d
commit 3aba91b360

View File

@@ -96,7 +96,9 @@ let
### is hard to adjust while screen is on. ### is hard to adjust while screen is on.
### note that only under alsa (`-ao=alsa`) does `ao-volume` actually correspond to system volume. ### note that only under alsa (`-ao=alsa`) does `ao-volume` actually correspond to system volume.
substituteInPlace src/uosc/main.lua \ substituteInPlace src/uosc/main.lua \
--replace-fail "mp.observe_property('volume'" "mp.observe_property('ao-volume'" --replace-fail \
"mp.observe_property('volume', 'number', create_state_setter('volume'))" \
"mp.observe_property('volume', 'number', update_ao_volume)"
substituteInPlace src/uosc/elements/Volume.lua \ substituteInPlace src/uosc/elements/Volume.lua \
--replace-fail "mp.commandv('set', 'volume'" "mp.commandv('set', 'ao-volume'" \ --replace-fail "mp.commandv('set', 'volume'" "mp.commandv('set', 'ao-volume'" \
--replace-fail "mp.set_property_native('volume'" "mp.set_property('ao-volume'" --replace-fail "mp.set_property_native('volume'" "mp.set_property('ao-volume'"
@@ -107,14 +109,20 @@ let
# in the meantime, just query the volume every tick (i.e. frame). # in the meantime, just query the volume every tick (i.e. frame).
# alternative is mpv's JSON IPC feature, where i could notify its socket whenever pipewire volume changes. # alternative is mpv's JSON IPC feature, where i could notify its socket whenever pipewire volume changes.
cat <<EOF >> src/uosc/main.lua cat <<EOF >> src/uosc/main.lua
function update_ao_volume() function update_ao_volume(_, vol)
local vol = mp.get_property('ao-volume') if vol == nil then
if vol ~= nil then -- vol will be nil if called manually, instead of via observe_property
vol = mp.get_property('ao-volume')
end
if vol == nil then
vol = 0
else
vol = tonumber(vol) vol = tonumber(vol)
if vol ~= state.volume then end
set_state('volume', vol)
request_render() if vol ~= state.volume then
end set_state('volume', vol)
request_render()
end end
end end
-- tick seems to occur on every redraw (even when volume is hidden). -- tick seems to occur on every redraw (even when volume is hidden).
@@ -124,9 +132,6 @@ let
mp.add_periodic_timer(2, update_ao_volume) mp.add_periodic_timer(2, update_ao_volume)
-- invoke immediately to ensure state.volume is non-nil -- invoke immediately to ensure state.volume is non-nil
update_ao_volume() update_ao_volume()
if state.volume == nil then
state.volume = 0
end
EOF EOF
''; '';
}); });