Fix invisible/flashing clock, map Protect's 8-camera preset, warn on gaps

Three separate faults made cameras "not load" and the clock misbehave.

Clock rendered as nothing, or flashed ~200ms/second. The time was drawn as
an ASS osd-overlay pushed over mpv IPC, but on mpv 0.35 + Mesa/V3D + sway an
OSD overlay is rendered only on the frame where its content *changes*. Every
layer reports success while this happens (mpv returns error:success,
vo-configured is true, and sway reports the window visible at the right
rect), so it looks like a stacking or font bug and is neither. Ruled out:
pushing at 20Hz (identical content is ignored, so it still only redrew when
the second flipped), osd-msg1, show-text, and --pause (mpv stops redrawing
entirely). Fonts were never the issue.

The time is now baked into every frame by a drawtext filter re-reading a
small file the ticker rewrites once a second, with two constraints that cost
real time to find and are pinned by tests:

- The canvas alpha must be > 0. A fully transparent canvas (black@0.0 with
  --alpha=yes) makes the glyphs inherit alpha 0 and the compositor draws
  nothing -- this was the original invisible clock. New clock
  background_opacity (default 0.45) is clamped in config *and* in args() so
  no code path can produce an invisible clock.
- Readahead must be off. drawtext stamps the time when a frame is
  *generated*, so buffering ahead makes the visible clock lag by the
  readahead and swallows text-file updates entirely.

Since the text now arrives through a file, the clock needs no IPC socket:
dropped --input-ipc-server, the ipcPath field, and the stale-socket removal.
assEscape goes with the ASS path.

`views import` produced layouts with holes. viewmap derived the grid from
the slot count alone and ignored Protect's `layout` field, so Protect's
asymmetric 8-camera preset (four 2x2 tiles plus a right column of four 1x1)
landed as 8 tiles in a 3x3 grid -- the bottom-right cell was simply empty
and rendered as a blank rectangle. That preset is now mapped exactly; other
counts keep the uniform GridForSlots fallback rather than guessing at
presets I have not observed. Import also warns when a mapping would leave
empty cells or references a camera missing from the config, so a silent hole
cannot reach the screen again.

Also:

- clock.corner gains bottom-center and top-center (centered horizontally,
  Margin still applies vertically).
- placeClock no longer re-issues `resize set` every tick. Re-asserting
  geometry on a correctly-sized window makes sway send a configure event,
  which makes mpv reallocate buffers and blank for a frame. New
  compositor.Raise re-asserts z-order only, which is all the 2s tick needs;
  geometry is re-placed only when it has actually drifted.
- README documents why the clock is drawn this way, the preset table and how
  to add another from `views dump`, and three troubleshooting entries for
  failure modes that all look like bugs: a blank tile whose mpv is running
  (a stale camera entry -- re-adopting a camera in Protect assigns a new id
  and often a slightly different name, and `discover` never prunes), cameras
  in `cameras:` not being on screen (only the active layout's tiles stream),
  and black bars inside tiles (non-16:9 grid cells; --panscan=1.0 crops to
  fill instead).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LYhTnkp7VzJ67THeicgfAQ
This commit is contained in:
Levi Woodard
2026-07-29 12:17:20 -06:00
parent caed091dc2
commit 81193f524c
10 changed files with 562 additions and 80 deletions

View File

@@ -31,18 +31,66 @@ func GridForSlots(n int) (cols, rows int) {
}
}
// LayoutFromView maps a Protect live view to a layout. Cameras are placed
// row-major, one per slot (the first camera of a cycling slot). Sizing uses a
// slot-count grid for now; asymmetric Protect presets need the `layout` int
// mapping. The returned layout is linked back to the view (ProtectView) so the
// daemon can re-sync it. Returns warnings for unmappable cameras.
// preset is a Protect arrangement: a base grid plus one cell placement per
// slot, in slot order. Camera is filled in by LayoutFromView.
type preset struct {
cols, rows int
cells []config.Tile
}
// presets holds the Protect arrangements that are NOT a uniform grid, keyed by
// slot count. Protect picks these presets by camera count, so a count that is
// absent here is a plain row-major grid and falls through to uniformPreset.
var presets = map[int]preset{
// Protect's 8-camera preset: four 2x2 tiles with a right-hand column of
// four 1x1 tiles. Numbers are slot order:
// 1 1 2 2 3
// 1 1 2 2 4
// 5 5 6 6 7
// 5 5 6 6 8
8: {cols: 5, rows: 4, cells: []config.Tile{
{Col: 0, Row: 0, ColSpan: 2, RowSpan: 2},
{Col: 2, Row: 0, ColSpan: 2, RowSpan: 2},
{Col: 4, Row: 0, ColSpan: 1, RowSpan: 1},
{Col: 4, Row: 1, ColSpan: 1, RowSpan: 1},
{Col: 0, Row: 2, ColSpan: 2, RowSpan: 2},
{Col: 2, Row: 2, ColSpan: 2, RowSpan: 2},
{Col: 4, Row: 2, ColSpan: 1, RowSpan: 1},
{Col: 4, Row: 3, ColSpan: 1, RowSpan: 1},
}},
}
// uniformPreset builds a plain row-major grid of 1x1 cells.
func uniformPreset(cols, rows int) preset {
p := preset{cols: cols, rows: rows}
for i := 0; i < cols*rows; i++ {
p.cells = append(p.cells, config.Tile{Col: i % cols, Row: i / cols, ColSpan: 1, RowSpan: 1})
}
return p
}
// presetForSlots returns the arrangement Protect uses for n slots.
func presetForSlots(n int) preset {
if p, ok := presets[n]; ok {
return p
}
cols, rows := GridForSlots(n)
return uniformPreset(cols, rows)
}
// LayoutFromView maps a Protect live view to a layout. Each slot's first camera
// (slots may cycle several) is placed into that slot's cell in the arrangement
// Protect uses for the slot count — see presets. The returned layout is linked
// back to the view (ProtectView) so the daemon can re-sync it. Returns warnings
// for unmappable cameras and for any arrangement that leaves the wall with
// empty cells, which would otherwise show as blank rectangles on screen.
func LayoutFromView(v protect.LiveView, idToName map[string]string) (config.Layout, []string) {
cols, rows := GridForSlots(len(v.Slots))
p := presetForSlots(len(v.Slots))
var tiles []config.Tile
var warns []string
for i, slot := range v.Slots {
if i >= cols*rows {
warns = append(warns, fmt.Sprintf("more slots than the %dx%d grid holds; extra dropped", cols, rows))
if i >= len(p.cells) {
warns = append(warns, fmt.Sprintf("more slots than the %dx%d grid holds; extra dropped", p.cols, p.rows))
break
}
if len(slot.Cameras) == 0 {
@@ -50,19 +98,51 @@ func LayoutFromView(v protect.LiveView, idToName map[string]string) (config.Layo
}
name := idToName[slot.Cameras[0]]
if name == "" {
warns = append(warns, fmt.Sprintf("slot %d camera %s not in config", i, slot.Cameras[0]))
warns = append(warns, fmt.Sprintf("slot %d camera %s not in config (run `discover`)", i, slot.Cameras[0]))
continue
}
tiles = append(tiles, config.Tile{Camera: name, Col: i % cols, Row: i / cols, ColSpan: 1, RowSpan: 1})
t := p.cells[i]
t.Camera = name
tiles = append(tiles, t)
}
if gaps := unfilled(p, tiles); gaps > 0 {
warns = append(warns, fmt.Sprintf("%d of %d cells in the %dx%d grid are empty and will show as blank areas",
gaps, p.cols*p.rows, p.cols, p.rows))
}
return config.Layout{
Name: Sanitize(v.Name),
Grid: fmt.Sprintf("%dx%d", cols, rows),
Grid: fmt.Sprintf("%dx%d", p.cols, p.rows),
Tiles: tiles,
ProtectView: v.Name,
}, warns
}
// unfilled counts grid cells no tile covers. A non-zero result means the wall
// has holes: either the preset does not tile its own grid or slots were skipped.
func unfilled(p preset, tiles []config.Tile) int {
if p.cols <= 0 || p.rows <= 0 {
return 0
}
covered := make([]bool, p.cols*p.rows)
for _, t := range tiles {
cs, rs := t.Span()
for r := t.Row; r < t.Row+rs; r++ {
for c := t.Col; c < t.Col+cs; c++ {
if r >= 0 && r < p.rows && c >= 0 && c < p.cols {
covered[r*p.cols+c] = true
}
}
}
}
n := 0
for _, ok := range covered {
if !ok {
n++
}
}
return n
}
// Sanitize turns a view name into a layout name (lowercase, dashed).
func Sanitize(s string) string {
s = strings.ToLower(strings.TrimSpace(s))

View File

@@ -0,0 +1,139 @@
package viewmap
import (
"strings"
"testing"
"github.com/lwoodard/rtsp-streamer/internal/protect"
)
// view builds a live view whose slots each hold one camera id.
func view(name string, ids ...string) protect.LiveView {
v := protect.LiveView{Name: name}
for _, id := range ids {
v.Slots = append(v.Slots, protect.LiveViewSlot{Cameras: []string{id}})
}
return v
}
// namesFor maps ids "c0".."cN-1" to themselves so tiles are easy to assert on.
func namesFor(n int) map[string]string {
m := map[string]string{}
for i := 0; i < n; i++ {
m[id(i)] = id(i)
}
return m
}
func id(i int) string { return string(rune('a' + i)) }
func ids(n int) []string {
out := make([]string, n)
for i := range out {
out[i] = id(i)
}
return out
}
// The 8-camera Protect preset is asymmetric: four 2x2 tiles plus a right-hand
// column of four 1x1 tiles, tiling a 5x4 grid exactly.
func TestLayoutFromView_EightSlotPreset(t *testing.T) {
got, warns := LayoutFromView(view("Office View", ids(8)...), namesFor(8))
if got.Grid != "5x4" {
t.Fatalf("grid = %q, want 5x4", got.Grid)
}
if len(warns) != 0 {
t.Errorf("unexpected warnings: %v", warns)
}
if got.ProtectView != "Office View" || got.Name != "office-view" {
t.Errorf("name/link = %q/%q", got.Name, got.ProtectView)
}
want := []struct{ col, row, cs, rs int }{
{0, 0, 2, 2}, {2, 0, 2, 2}, {4, 0, 1, 1}, {4, 1, 1, 1},
{0, 2, 2, 2}, {2, 2, 2, 2}, {4, 2, 1, 1}, {4, 3, 1, 1},
}
if len(got.Tiles) != len(want) {
t.Fatalf("got %d tiles, want %d", len(got.Tiles), len(want))
}
for i, w := range want {
tile := got.Tiles[i]
cs, rs := tile.Span()
if tile.Col != w.col || tile.Row != w.row || cs != w.cs || rs != w.rs {
t.Errorf("tile %d = (%d,%d)+%dx%d, want (%d,%d)+%dx%d",
i, tile.Col, tile.Row, cs, rs, w.col, w.row, w.cs, w.rs)
}
if tile.Camera != id(i) {
t.Errorf("tile %d camera = %q, want %q", i, tile.Camera, id(i))
}
}
}
// The 8-slot preset must leave no holes; that was the original bug (8 tiles
// dropped into a 3x3 grid left the bottom-right cell blank).
func TestLayoutFromView_EightSlotsTileGridExactly(t *testing.T) {
got, _ := LayoutFromView(view("Office View", ids(8)...), namesFor(8))
if n := unfilled(presetForSlots(8), got.Tiles); n != 0 {
t.Errorf("%d cells left empty, want 0", n)
}
}
// Counts without a preset keep the uniform row-major grid.
func TestLayoutFromView_UniformFallback(t *testing.T) {
got, warns := LayoutFromView(view("Default", ids(9)...), namesFor(9))
if got.Grid != "3x3" {
t.Fatalf("grid = %q, want 3x3", got.Grid)
}
if len(warns) != 0 {
t.Errorf("unexpected warnings: %v", warns)
}
for i, tile := range got.Tiles {
if tile.Col != i%3 || tile.Row != i/3 {
t.Errorf("tile %d at (%d,%d), want (%d,%d)", i, tile.Col, tile.Row, i%3, i/3)
}
}
}
// A count whose uniform grid is larger than the slot count leaves holes, and
// the caller must be told rather than silently shipping a wall with blanks.
func TestLayoutFromView_WarnsOnHoles(t *testing.T) {
got, warns := LayoutFromView(view("Seven", ids(7)...), namesFor(7))
if got.Grid != "3x3" {
t.Fatalf("grid = %q, want 3x3", got.Grid)
}
if !hasWarn(warns, "empty") {
t.Errorf("expected an empty-cell warning, got %v", warns)
}
}
// An unknown camera id is reported and its cell left out.
func TestLayoutFromView_WarnsOnUnknownCamera(t *testing.T) {
got, warns := LayoutFromView(view("Office View", ids(8)...), namesFor(7))
if len(got.Tiles) != 7 {
t.Errorf("got %d tiles, want 7", len(got.Tiles))
}
if !hasWarn(warns, "not in config") {
t.Errorf("expected an unknown-camera warning, got %v", warns)
}
}
func TestUnfilledCountsUncoveredCells(t *testing.T) {
p := uniformPreset(3, 3)
if n := unfilled(p, nil); n != 9 {
t.Errorf("empty layout: got %d uncovered, want 9", n)
}
full, _ := LayoutFromView(view("Default", ids(9)...), namesFor(9))
if n := unfilled(p, full.Tiles); n != 0 {
t.Errorf("full 3x3: got %d uncovered, want 0", n)
}
}
func hasWarn(warns []string, substr string) bool {
for _, w := range warns {
if strings.Contains(w, substr) {
return true
}
}
return false
}