Reconnect streams in-process; fix for_window IPC rules

Root cause of the periodic drops (found via the new logs): mpv was
exiting cleanly (err=nil, exit 0) with its status line at 100% — i.e.
end-of-stream. UniFi Protect closes some cameras' RTSP connections every
20-40s; with --keep-open=no --idle=no mpv treated that as the file
ending, exited, and the supervisor relaunched it ~3s later (the visible
drop).

- Add --loop-file=inf so mpv reopens the stream the instant it EOFs,
  in the same process and window: sub-second recovery, no teardown,
  no supervisor bounce. The supervisor now only fires for real crashes.
- Clean up the logged exit "reason": strip ANSI escapes and skip mpv's
  transient A/V status prints, so a genuine error surfaces instead of
  "\x1b[KV: 00:00:35 / 00:00:35 (100%)". Tests cover the parsing.

Also fold in the for_window IPC fix: sway's IPC parser splits comma-
joined command lists at the top level and does not fold the continuation
into for_window (unlike the config-file parser), so the map-time
placement rules errored with "Only views can have borders" and never
installed. Register each command separately; drop the redundant
border none (the kiosk config already sets default_border none).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Levi Woodard
2026-07-02 12:54:02 -05:00
parent be1b53ae04
commit ed9814ab4e
4 changed files with 105 additions and 21 deletions

View File

@@ -179,23 +179,37 @@ func (c *Client) Place(ctx context.Context, pid int, r Rect) error {
return c.run(ctx, cmd)
}
// PrepareForMPV installs global rules so every mpv window is borderless and
// floating the moment it maps, avoiding a flash of tiled/bordered video before
// Place runs. Safe to call repeatedly.
// PrepareForMPV installs a global rule so every mpv window maps floating,
// ready for the daemon to position. Borders are already off via the kiosk
// config's `default_border none`, so this rule is a single command: sway's
// IPC parser splits a comma-joined command list at the top level (it does NOT
// fold the continuation into for_window the way the config-file parser does),
// so a multi-command for_window must be registered one command per call.
// Safe to call repeatedly.
func (c *Client) PrepareForMPV(ctx context.Context) error {
return c.run(ctx, `for_window [app_id="mpv"] floating enable, border none`)
return c.run(ctx, `for_window [app_id="mpv"] floating enable`)
}
// PlaceOnMap installs a for_window rule that positions any window with the
// given title at rect the moment it maps. This is what keeps a restarting
// stream from flashing at the wrong place/size: sway applies the geometry
// synchronously at map time, before the daemon's corrective loop ever sees
// the window. Re-installing a rule for the same title replaces it. The title
// is anchored (^...$) so slot-1 never matches slot-10.
// PlaceOnMap installs for_window rules that position and size any window with
// the given title the moment it maps — so a restarting stream lands in its
// cell without a wrong-place flash, before the corrective loop ever runs. The
// title is anchored (^...$) so slot-1 never matches slot-10. Because sway's
// IPC command parser splits on commas at the top level, each rule carries a
// single command and is registered separately (a comma-joined for_window would
// silently drop everything after the first command and run the rest
// immediately against the focused container). Re-registering a rule for the
// same criteria replaces it.
func (c *Client) PlaceOnMap(ctx context.Context, title string, r Rect) error {
cmd := fmt.Sprintf(
`for_window [title="^%s$"] floating enable, border none, move absolute position %d %d, resize set %d %d`,
title, r.X, r.Y, r.W, r.H,
)
return c.run(ctx, cmd)
crit := fmt.Sprintf(`[title="^%s$"]`, title)
cmds := []string{
fmt.Sprintf("for_window %s floating enable", crit),
fmt.Sprintf("for_window %s move absolute position %d %d", crit, r.X, r.Y),
fmt.Sprintf("for_window %s resize set %d %d", crit, r.W, r.H),
}
for _, cmd := range cmds {
if err := c.run(ctx, cmd); err != nil {
return err
}
}
return nil
}