From be31fb5eab1ef975e9fa113cce91e677eca2c557 Mon Sep 17 00:00:00 2001 From: diviocity Date: Sat, 7 Nov 2026 18:03:44 +0100 Subject: [PATCH] fix playback (updated V video, fix pipewire audio) --- AGENTS.md | 17 ++++++++++++++--- libvendetta/draw.v | 8 ++++++++ src/media.v | 22 +++++++++++++++------- src/player.v | 22 ++++++++++++++++++++++ 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 6b93cb5..348ad1f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -449,7 +449,12 @@ time the widget's surface id is handed to the GL renderer, because it needs the host's GL context. On that first call the widget also claims its wake slot, registers the render update callback, spawns the relay, and flushes its `startup` queue into mpv (`loadfile ... replace` for the first, -`append-play` for the rest). +`append-play` for the rest). ui2 calls the surface renderer one frame after +the `opengl` element first appears, and an on-demand window asks for no second +frame by itself, so `build()` asks for one paint (`ui2.request_paint`) while +the render context is still nil. Without that ask, startup depended on a +stray X event or timer landing after the first frame. When none did, nothing +loaded and playback never started. `destroy()` is the teardown twin of the first draw: it parks and joins the relay (`stop_relay` — flag + one bump empties the parked wait so a destroy @@ -867,7 +872,10 @@ Every window event string goes through `handle(event)` then - `track::` → writes `aid`/`sid`/`vid` async (50). - `chapter:` → `chapter` write (51). -- `audio_device:` → `audio-device` write (56). +- `audio_device:` → `audio-device` write (56). The Output device + menu lists only the devices `device_fits_ao` allows under the current + server. A pinned `ao` ignores another driver's `driver/device` and keeps + playing on its own default sink. - `ao_server:` → app mirror + `ao` write (`''` for auto, 57), an immediate `refresh_media_async(true)` (the device list belongs to the new server; the device property silently changes once the server runs) + menus. @@ -1064,7 +1072,10 @@ which is slow and can block for a long time against a dead server, so the per-file and per-selection bursts never ask for devices; only the start-up pass and the audio-server/output-device writes do. Results arrive on `media_chan` and `drain_media` applies them (`set_menu_bar` follows; -`media_read_busy` released; a dirty flag re-reads without devices). +`media_read_busy` released; a dirty flag re-reads without devices). Each +`MediaData` carries `read_devices`, and only a read that asked for devices +replaces `p.devices`. Before this, the file-load read emptied the Output +device menu. ### 7.12 Settings, queue, migration diff --git a/libvendetta/draw.v b/libvendetta/draw.v index 63e9b2e..ee7c0f8 100644 --- a/libvendetta/draw.v +++ b/libvendetta/draw.v @@ -763,6 +763,14 @@ pub fn (mut p Player) build(frame ui2.Rect) ui2.Element { video_rect := ui2.rect(0, 0, frame.width, video_h) // Overlay controls hide on a rest deadline; docked controls never hide. controls_shown := p.strip_shown() + // ui2 hands an opengl element to the surface renderer one frame after it + // first appears, and the render context, the relay and the startup files + // all wait for that first draw. An on-demand window asks for no second + // frame by itself, so until the surface has drawn, every build asks for + // one paint; once the context is up, the relay paces the frames. + if p.render_ctx == unsafe { nil } { + ui2.request_paint() + } mut children := [ui2.opengl(video_surface_id, video_rect)] if !controls_shown { return ui2.view(root_id, frame, ui2.BoxStyle{ transparent: true }, children) diff --git a/src/media.v b/src/media.v index 1edc1b7..2afb915 100644 --- a/src/media.v +++ b/src/media.v @@ -599,10 +599,13 @@ fn (mut p Player) show_thumb() { // slow, can block a long time against a dead server, and rarely changes. The // file-load and track bursts therefore carry no devices; the device readers — // the start-up pass and the audio-device/ao writes — ask for them. +// read_devices tells drain_media whether devices is a fresh list or merely +// left unread: a read without the devices must keep the menu's last list. struct MediaData { - tracks []Track - chapters []Chapter - devices []AudioDevice + tracks []Track + chapters []Chapter + devices []AudioDevice + read_devices bool } // read_media_worker runs on its own thread. libmpv core calls are safe from @@ -655,9 +658,10 @@ fn read_media_worker(ctx voidptr, res chan MediaData, with_devices bool) { } } res <- MediaData{ - tracks: tracks - chapters: chapters - devices: devices + tracks: tracks + chapters: chapters + devices: devices + read_devices: with_devices } } @@ -694,7 +698,11 @@ fn (mut p Player) drain_media() { } p.tracks = data.tracks p.chapters = data.chapters - p.devices = data.devices + // The per-file reads skip the devices; only a read that asked for + // them replaces the list, or every file load would empty the menu. + if data.read_devices { + p.devices = data.devices + } p.media_read_busy = false ui2.set_menu_bar(p.menus()) if p.media_dirty { diff --git a/src/player.v b/src/player.v index a63772c..6074185 100644 --- a/src/player.v +++ b/src/player.v @@ -1775,6 +1775,12 @@ fn (mut p Player) handle_media(event string) bool { // belongs to the new server, so this explicit hand-off re-reads it // (the device property silently changes once the server runs). mpv.set_string_async(p.mpv_ctx, 57, 'ao', if name == 'auto' { '' } else { name }) + // A device of the old server means nothing to the new one: mpv + // would play on the new server's default while the menu still + // ticked the old device, so the choice goes back to auto. + if !device_fits_ao(p.audio_device, name) { + mpv.set_string_async(p.mpv_ctx, 56, 'audio-device', 'auto') + } p.refresh_media_async(true) ui2.set_menu_bar(p.menus()) } @@ -2032,6 +2038,19 @@ fn (p &Player) mpv_menu_items() []ui2.MenuItem { return items } +// device_fits_ao reports whether mpv would use the audio device `name` under +// the audio server `ao`. mpv names devices `driver/device` (`pulse/...`, +// `pipewire/...`), plus `auto` and a bare driver name for that driver's +// default. Under autoprobing a prefixed device picks its own driver, so every +// device fits. A pinned server ignores other drivers' devices and keeps +// playing on its default sink. +fn device_fits_ao(name string, ao string) bool { + if ao == 'auto' || name == 'auto' { + return true + } + return name == ao || name.starts_with('${ao}/') +} + // audio_server_names lists the audio servers the menus offer and the --ao // flag accepts — the outputs this platform's mpv can be expected to have. // 'auto' is mpv's own autoprobing, which tries its drivers in built-in order; @@ -2078,6 +2097,9 @@ fn (p &Player) menus() []ui2.Menu { mut devices := []ui2.MenuItem{} for device in p.devices { + if !device_fits_ao(device.name, p.ao) { + continue + } devices << ui2.menu_check_item('audio_device:${device.name}', device.description, device.name == p.audio_device) } -- 2.56.1