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

@@ -13,6 +13,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"
"syscall"
@@ -74,6 +75,13 @@ func (p *Player) args() []string {
"--cursor-autohide=always",
"--no-border",
"--fullscreen=no", // we tile via the compositor, not fullscreen
// Reconnect in-process when the RTSP stream ends. UniFi Protect closes
// a camera's RTSP connection periodically (every 20-40s on some
// models); without this mpv hits EOF, exits cleanly, and the
// supervisor relaunches it ~3s later — a visible drop. loop-file=inf
// reopens the same URL the instant it ends, keeping the window alive,
// so recovery is sub-second with no teardown.
"--loop-file=inf",
// Fit any camera into its tile regardless of the camera's aspect:
// * keepaspect=yes → letterbox the video, never crop/stretch
// * keepaspect-window=no → do NOT reshape the window to the video's
@@ -282,20 +290,45 @@ func (p *Player) Healthy() bool {
return reply["error"] == "success"
}
// lastLine returns the most recent non-empty stderr line mpv emitted, trimmed,
// or "" if there was none. mpv prints its fatal error last, so the tail is the
// useful part.
// ansiRE matches terminal escape sequences mpv sprinkles into its output.
var ansiRE = regexp.MustCompile(`\x1b\[[0-9;?]*[ -/]*[@-~]`)
// lastLine returns the most useful recent stderr line mpv emitted: the last
// line that is not one of mpv's periodic playback-status prints (e.g.
// "V: 00:00:35 / 00:00:35 (100%)"), with ANSI escapes stripped. A real error
// (connection refused, unsupported codec, 401) is what we want to surface; the
// status line is only the fallback when nothing else was printed.
func lastLine(w *logbuf.Writer) string {
if w == nil {
return ""
}
lines := w.Lines(0)
fallback := ""
for i := len(lines) - 1; i >= 0; i-- {
if s := strings.TrimSpace(lines[i]); s != "" {
return s
s := strings.TrimSpace(ansiRE.ReplaceAllString(lines[i], ""))
if s == "" {
continue
}
if fallback == "" {
fallback = s
}
if isStatusLine(s) {
continue
}
return s
}
return fallback
}
// isStatusLine reports whether s is one of mpv's transient A/V progress prints
// rather than a substantive message.
func isStatusLine(s string) bool {
for _, p := range []string{"V:", "A:", "AV:", "(Paused)", "(Buffering)"} {
if strings.HasPrefix(s, p) {
return true
}
}
return ""
return false
}
func sleep(ctx context.Context, d time.Duration) bool {

View File

@@ -0,0 +1,31 @@
package player
import (
"fmt"
"testing"
"github.com/lwoodard/rtsp-streamer/internal/logbuf"
)
func TestLastLinePrefersRealMessageOverStatus(t *testing.T) {
w := logbuf.New(20)
fmt.Fprintln(w, "Failed to open rtsps://host/x: Connection refused")
fmt.Fprint(w, "\x1b[KV: 00:00:35 / 00:00:35 (100%)\n") // ANSI-wrapped status
if got := lastLine(w); got != "Failed to open rtsps://host/x: Connection refused" {
t.Errorf("lastLine = %q, want the error line", got)
}
}
func TestLastLineFallsBackToStatusWhenOnlyStatus(t *testing.T) {
w := logbuf.New(20)
fmt.Fprint(w, "\x1b[KV: 00:00:20 / 00:00:20 (100%)\n")
if got := lastLine(w); got != "V: 00:00:20 / 00:00:20 (100%)" {
t.Errorf("lastLine = %q, want the stripped status line", got)
}
}
func TestLastLineNil(t *testing.T) {
if got := lastLine(nil); got != "" {
t.Errorf("lastLine(nil) = %q, want empty", got)
}
}