Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/added-stream-audio-tracker-sounds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The Activity Monitor Twitch streamer now carries the tracker's merge-zap and new-item-buzz sounds: Chromium plays into a PulseAudio null sink that ffmpeg captures (`STREAM_AUDIO=silent` restores the silent track), and kiosk mode (`?kiosk=1`) turns tracker sounds on by default without a gesture or localStorage write (`&sound=off` keeps it quiet).
12 changes: 8 additions & 4 deletions src/deploy/stream/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ RUN go mod init hive-stream/authproxy >/dev/null 2>&1 \

FROM ${BASE_REGISTRY}/debian:bookworm-slim@sha256:7c7b2c966bc9ee8cedfeef67e0e279108992c77681fa595db4a9d65c06ccc587
ENV DEBIAN_FRONTEND=noninteractive
# No PulseAudio on purpose: Twitch requires an audio track, which ffmpeg
# synthesises from anullsrc; Chromium runs --mute-audio.
# PulseAudio provides a null sink ("hive") that Chromium plays the tracker's
# merge-zap / new-item-buzz sounds into; ffmpeg captures its monitor so the
# stream carries them. STREAM_AUDIO=silent (entrypoint.sh) restores the old
# anullsrc + --mute-audio behaviour without PulseAudio running.
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates curl tini procps \
xvfb x11-utils xdotool \
pulseaudio pulseaudio-utils \
chromium \
ffmpeg \
fonts-dejavu-core fonts-noto-core fonts-noto-color-emoji fonts-liberation \
Expand All @@ -38,12 +41,13 @@ RUN apt-get update \
COPY --from=authproxy /out/authproxy /usr/local/bin/authproxy
COPY src/deploy/stream/entrypoint.sh src/deploy/stream/healthcheck.sh /usr/local/bin/
RUN chmod 0755 /usr/local/bin/entrypoint.sh /usr/local/bin/healthcheck.sh \
&& chromium --version && ffmpeg -version | head -1 && Xvfb -help >/dev/null 2>&1
&& chromium --version && ffmpeg -version | head -1 && Xvfb -help >/dev/null 2>&1 \
&& pulseaudio --version && ffmpeg -hide_banner -devices 2>/dev/null | grep -q pulse

USER stream
ENV HOME=/home/stream RUN_DIR=/run/stream \
DASHBOARD_PATH="/?kiosk=1#section-activity-monitor-section" \
TWITCH_INGEST=rtmp://live.twitch.tv/app \
VIDEO_SIZE=1920x1080 FPS=30 BITRATE=4500k RELOAD_EVERY=30m
VIDEO_SIZE=1920x1080 FPS=30 BITRATE=4500k RELOAD_EVERY=30m STREAM_AUDIO=pulse
HEALTHCHECK --interval=30s --timeout=5s --start-period=90s --retries=3 CMD ["/usr/local/bin/healthcheck.sh"]
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/entrypoint.sh"]
5 changes: 3 additions & 2 deletions src/deploy/stream/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Twitch stream. Operator guide, Twitch-side setup, and troubleshooting:
| File | Purpose |
| --- | --- |
| `Dockerfile` | `debian:bookworm-slim` + Xvfb, Chromium, ffmpeg, xdotool, tini; builds `authproxy` in a `golang` stage. Build context is the **repo root**. Published as `ghcr.io/hivecommons/hive-stream:<sha>` / `:v5-latest` by `.github/workflows/stream-image.yml`. |
| `entrypoint.sh` | Supervisor: Xvfb → authproxy → Chromium `--kiosk` (restart with backoff) → periodic reload (`RELOAD_EVERY`) → ffmpeg `x11grab` + `anullsrc` → libx264 CBR → RTMP. Masks the stream key in ffmpeg output. |
| `entrypoint.sh` | Supervisor: Xvfb → authproxy → Chromium `--kiosk` (restart with backoff) → periodic reload (`RELOAD_EVERY`) → PulseAudio null sink `hive` → ffmpeg `x11grab` + `pulse hive.monitor` (or `anullsrc` with `STREAM_AUDIO=silent`) → libx264 CBR + AAC → RTMP. Masks the stream key in ffmpeg output. |
| `authproxy/` | Loopback reverse proxy (stdlib Go) that adds `Authorization: Bearer $DASHBOARD_TOKEN` to every dashboard request so the token is never in a URL, in Chromium's history, or on screen. Logs when the upstream answers with a login redirect. |
| `healthcheck.sh` | Chromium alive + ffmpeg progress file fresher than 20 s with `frame>0`. Used by the image `HEALTHCHECK` and the Deployment's probes. |
| `docker-compose.yaml`, `.env.example` | Run the same image from a laptop. |
Expand All @@ -20,7 +20,8 @@ Twitch stream. Operator guide, Twitch-side setup, and troubleshooting:
| --- | --- | --- |
| `DASHBOARD_URL` | required | Dashboard origin (`http://hive.<ns>.svc.cluster.local:3002` in-cluster, or the public route). |
| `DASHBOARD_TOKEN` | — | Spoke `dashboard.auth_token` (in-cluster) or a GitHub token of a hub user with access (public route). |
| `DASHBOARD_PATH` | `/?kiosk=1#section-activity-monitor-section` | Path Chromium opens. `?kiosk=1` is the dashboard's unattended display mode. |
| `DASHBOARD_PATH` | `/?kiosk=1#section-activity-monitor-section` | Path Chromium opens. `?kiosk=1` is the dashboard's unattended display mode; tracker sounds are on there unless `&sound=off` is added. |
| `STREAM_AUDIO` | `pulse` | `pulse` captures the tracker sounds via a PulseAudio null sink; `silent` mutes Chromium and streams `anullsrc` silence. |
| `TWITCH_STREAM_KEY` | — | Primary Stream key. |
| `TWITCH_INGEST` | `rtmp://live.twitch.tv/app` | Ingest; pick a closer one from Twitch's ingest list if needed. |
| `VIDEO_SIZE` / `FPS` / `BITRATE` / `X264_PRESET` | `1920x1080` / `30` / `4500k` / `veryfast` | Encode settings. Keyframe interval is `2×FPS`. |
Expand Down
58 changes: 51 additions & 7 deletions src/deploy/stream/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#!/usr/bin/env bash
# Headless Activity Monitor streamer (hivecommons/hive#15594).
#
# Xvfb ─► Chromium --kiosk ─► (loopback authproxy) ─► dashboard
# └────────────── ffmpeg x11grab + silent audio ─► Twitch RTMP
# Xvfb ──────► Chromium --kiosk ─► (loopback authproxy) ─► dashboard
# │ │ tracker sounds (Web Audio)
# │ ▼
# │ PulseAudio null sink "hive" ─► hive.monitor
# │ │
# └── ffmpeg x11grab ◄────────────────────┘ (-f pulse) ─► Twitch RTMP
#
# STREAM_AUDIO=pulse (default) captures the dashboard's merge-zap / new-item
# buzz through the null sink; STREAM_AUDIO=silent skips PulseAudio, mutes
# Chromium and synthesises silence with anullsrc (Twitch still needs a track).
#
# Every process is supervised here: Chromium and ffmpeg are restarted with
# backoff when they exit, the page is reloaded every RELOAD_EVERY so a rolled
Expand All @@ -24,6 +32,8 @@ FPS="${FPS:-30}"
BITRATE="${BITRATE:-4500k}"
X264_PRESET="${X264_PRESET:-veryfast}"
RELOAD_EVERY="${RELOAD_EVERY:-30m}"
STREAM_AUDIO="${STREAM_AUDIO:-pulse}" # pulse | silent
PULSE_SINK_NAME="${PULSE_SINK_NAME:-hive}"
DISPLAY_NUM="${DISPLAY_NUM:-:99}"
AUTHPROXY_LISTEN="${AUTHPROXY_LISTEN:-127.0.0.1:8099}"
RUN_DIR="${RUN_DIR:-/run/stream}"
Expand All @@ -32,8 +42,14 @@ BACKOFF_MAX=60
KEYINT=$(( FPS * 2 )) # Twitch wants a 2 s keyframe interval
BUFSIZE="$(( ${BITRATE%k} * 2 ))k"

case "$STREAM_AUDIO" in pulse|silent) ;; *) echo "STREAM_AUDIO must be 'pulse' or 'silent' (got '$STREAM_AUDIO')" >&2; exit 1 ;; esac

export DISPLAY="$DISPLAY_NUM" AUTHPROXY_LISTEN DASHBOARD_URL DASHBOARD_TOKEN
mkdir -p "$RUN_DIR/profile"; rm -f "$RUN_DIR/done"
# Per-user PulseAudio under RUN_DIR: the pod's only writable volume besides
# HOME, and the socket path Chromium and ffmpeg both resolve via PULSE_SERVER.
export XDG_RUNTIME_DIR="$RUN_DIR/pulse" PULSE_SERVER="unix:$RUN_DIR/pulse/native"
mkdir -p "$XDG_RUNTIME_DIR"; chmod 0700 "$XDG_RUNTIME_DIR"
PROGRESS="$RUN_DIR/ffmpeg-progress.txt"
: > "$PROGRESS"
log() { printf '%s stream: %s\n' "$(date -u +%FT%TZ)" "$*"; }
Expand All @@ -49,6 +65,29 @@ for _ in $(seq 1 50); do xdpyinfo -display "$DISPLAY_NUM" >/dev/null 2>&1 && bre
xdpyinfo -display "$DISPLAY_NUM" >/dev/null 2>&1 || { log "Xvfb failed to start"; exit 1; }
log "Xvfb up on $DISPLAY_NUM (${VIDEO_SIZE})"

# ---- 1b. PulseAudio null sink (STREAM_AUDIO=pulse) --------------------------
if [ "$STREAM_AUDIO" = pulse ]; then
# Foreground child under the supervisor (not --start/--daemonize, which
# drops the --load list). -n: no default.pa, so no udev/ALSA probing in a
# pod; only the native socket and the null sink are loaded.
# --exit-idle-time=-1 keeps it alive between Chromium restarts.
pulseaudio --exit-idle-time=-1 --disallow-exit --realtime=no --high-priority=no \
--log-target=stderr --log-level=error -n \
--load="module-native-protocol-unix socket=$RUN_DIR/pulse/native" \
--load="module-null-sink sink_name=$PULSE_SINK_NAME sink_properties=device.description=$PULSE_SINK_NAME" \
> >(sed -u 's/^/pulseaudio: /') 2>&1 &
pids+=($!)
for _ in $(seq 1 50); do pactl info >/dev/null 2>&1 && break; sleep 0.2; done
pactl info >/dev/null 2>&1 || { log "pulseaudio failed to start"; exit 1; }
pactl list sinks short | awk '{print $2}' | grep -qx "$PULSE_SINK_NAME" \
|| pactl load-module module-null-sink "sink_name=$PULSE_SINK_NAME" "sink_properties=device.description=$PULSE_SINK_NAME" >/dev/null
for _ in $(seq 1 50); do pactl list sinks short | awk '{print $2}' | grep -qx "$PULSE_SINK_NAME" && break; sleep 0.2; done
pactl list sinks short | awk '{print $2}' | grep -qx "$PULSE_SINK_NAME" || { log "pulseaudio sink $PULSE_SINK_NAME did not appear"; exit 1; }
pactl set-default-sink "$PULSE_SINK_NAME"
pactl set-sink-mute "$PULSE_SINK_NAME" 0; pactl set-sink-volume "$PULSE_SINK_NAME" 100%
log "pulseaudio up; default sink $PULSE_SINK_NAME (ffmpeg captures ${PULSE_SINK_NAME}.monitor)"
fi

# ---- 2. authproxy (adds Authorization: Bearer, token stays off-screen) ------
authproxy -listen "$AUTHPROXY_LISTEN" &
pids+=($!)
Expand All @@ -58,14 +97,16 @@ KIOSK_URL="http://$AUTHPROXY_LISTEN$DASHBOARD_PATH"
log "authproxy up; kiosk URL $KIOSK_URL -> ${DASHBOARD_URL%%\?*} (token $([ -n "$DASHBOARD_TOKEN" ] && echo set || echo UNSET))"

# ---- 3. Chromium kiosk, restarted with backoff ------------------------------
CHROMIUM_AUDIO_FLAGS=()
[ "$STREAM_AUDIO" = silent ] && CHROMIUM_AUDIO_FLAGS=(--mute-audio)
chromium_loop() {
local backoff=$BACKOFF_MIN
while :; do
local started=$SECONDS
log "chromium starting"
chromium \
--kiosk --no-first-run --no-default-browser-check --disable-gpu \
--autoplay-policy=no-user-gesture-required --mute-audio \
--autoplay-policy=no-user-gesture-required ${CHROMIUM_AUDIO_FLAGS[@]+"${CHROMIUM_AUDIO_FLAGS[@]}"} \
--window-size="${VIDEO_SIZE/x/,}" --window-position=0,0 --force-device-scale-factor=1 \
--disable-infobars --disable-session-crashed-bubble --noerrdialogs --hide-scrollbars \
--disable-features=TranslateUI,MediaRouter --check-for-update-interval=31536000 \
Expand Down Expand Up @@ -102,6 +143,9 @@ sink() {
*) printf -- '%s' "$STREAM_OUTPUT" ;;
esac
}
# Audio input for ffmpeg: the null sink's monitor, or synthesised silence.
AUDIO_IN=(-f lavfi -i "anullsrc=channel_layout=stereo:sample_rate=44100")
[ "$STREAM_AUDIO" = pulse ] && AUDIO_IN=(-thread_queue_size 1024 -f pulse -i "${PULSE_SINK_NAME}.monitor")
ffmpeg_loop() {
local backoff=$BACKOFF_MIN out limit=() rc=0
out="$(sink)" || exit 1
Expand All @@ -110,15 +154,15 @@ ffmpeg_loop() {
sleep 5
while :; do
local started=$SECONDS
log "ffmpeg starting (${VIDEO_SIZE}@${FPS}, ${BITRATE} CBR, keyint ${KEYINT}, sink ${out%%/*}…)"
log "ffmpeg starting (${VIDEO_SIZE}@${FPS}, ${BITRATE} CBR, keyint ${KEYINT}, audio ${STREAM_AUDIO}, sink ${out%%/*}…)"
# shellcheck disable=SC2086
ffmpeg -nostdin -y -hide_banner -loglevel warning -progress "$PROGRESS" \
-f x11grab -framerate "$FPS" -video_size "$VIDEO_SIZE" -draw_mouse 0 -i "$DISPLAY_NUM" \
-f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-thread_queue_size 1024 -f x11grab -framerate "$FPS" -video_size "$VIDEO_SIZE" -draw_mouse 0 -i "$DISPLAY_NUM" \
"${AUDIO_IN[@]}" \
-c:v libx264 -preset "$X264_PRESET" -tune zerolatency -pix_fmt yuv420p \
-b:v "$BITRATE" -minrate "$BITRATE" -maxrate "$BITRATE" -bufsize "$BUFSIZE" \
-g "$KEYINT" -keyint_min "$KEYINT" -sc_threshold 0 -r "$FPS" \
-c:a aac -b:a 128k -ar 44100 -shortest "${limit[@]}" \
-c:a aac -b:a 128k -ac 2 -ar 44100 -shortest "${limit[@]}" \
$out > >(sed -u "s#${TWITCH_STREAM_KEY:-__none__}#***#g") 2>&1 && rc=0 || rc=$?
if (( STREAM_SECONDS > 0 )); then
if (( rc != 0 )); then log "ffmpeg bounded run FAILED (exit $rc)"; exit "$rc"; fi
Expand Down
2 changes: 2 additions & 0 deletions src/deploy/stream/k8s/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ data:
BITRATE: "4500k"
X264_PRESET: "veryfast"
RELOAD_EVERY: "30m"
# pulse: tracker sounds via a PulseAudio null sink; silent: anullsrc + --mute-audio.
STREAM_AUDIO: "pulse"
18 changes: 17 additions & 1 deletion src/docs/streaming-the-activity-monitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,25 @@ Deployment hive-stream (ns hive-stream, 1 replica)
Xvfb :99 1920x1080
authproxy 127.0.0.1:8099 ──Authorization: Bearer──▶ hive dashboard Service :3002
Chromium --kiosk http://127.0.0.1:8099/?kiosk=1#section-activity-monitor-section
ffmpeg x11grab 30 fps + silent audio ──libx264 4500 kbps CBR──▶ rtmp://live.twitch.tv/app/<key>
└─ tracker sounds ──▶ PulseAudio null sink "hive" (hive.monitor)
ffmpeg x11grab 30 fps + pulse hive.monitor ──libx264 4500 kbps CBR + AAC 128 kbps──▶ rtmp://live.twitch.tv/app/<key>
```

### Sound

The stream carries the tracker's sounds — the zap when a PR merges and the
buzz when a new issue or PR opens. In kiosk mode they are **on by default**
(a regular browser tab keeps them opt-in); Chromium plays them into a
PulseAudio null sink inside the container and ffmpeg captures that sink's
monitor. Two knobs:

- `DASHBOARD_PATH="/?kiosk=1&sound=off#section-activity-monitor-section"`
keeps the dashboard silent (the stream still carries a real, silent audio
track). Append `&theme=light` or other display flags the same way.
- `STREAM_AUDIO=silent` skips PulseAudio entirely: Chromium runs
`--mute-audio` and ffmpeg synthesises silence with `anullsrc`, the
pre-sound behaviour. Default is `STREAM_AUDIO=pulse`.

Nothing here touches Twitch or the cluster until **you** create the Secrets and
apply the overlay — the steps below are all yours to run.

Expand Down
51 changes: 51 additions & 0 deletions src/pkg/dashboard/kiosk_mode_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dashboard

import (
"os/exec"
"strings"
"testing"
)
Expand Down Expand Up @@ -32,3 +33,53 @@ func TestKioskModeWiring(t *testing.T) {
t.Error("kiosk mode must not persist state in localStorage")
}
}

// TestKioskSoundDefault guards the streamer's audio path: in kiosk mode the
// tracker sounds are on unless the URL says `&sound=off`, the context is
// resumed at init without waiting for a gesture (the streamer's Chromium
// allows autoplay), the held-back toast never reaches the stream, and no
// sound/volume preference is written to localStorage.
func TestKioskSoundDefault(t *testing.T) {
html := indexHTML(t)
for _, want := range []string{
`sound=new URL(window.location.href).searchParams.get('sound')!=='off';`,
`window.HIVE_KIOSK_SOUND=kiosk&&sound;`,
`window.hiveKioskSoundOn=function(){ return !!window.HIVE_KIOSK_SOUND; };`,
} {
if !strings.Contains(html, want) {
t.Errorf("index.html missing kiosk sound wiring %q", want)
}
}
if init := jsFunc(t, html, "stageTrackerSoundInit"); !strings.Contains(init, "if (window.hiveKioskMode && window.hiveKioskMode()) { stageTrackerKioskSoundInit(); return; }") {
t.Errorf("stageTrackerSoundInit must branch to the kiosk path before touching localStorage:\n%s", init)
}
kioskInit := jsFunc(t, html, "stageTrackerKioskSoundInit")
if strings.Contains(kioskInit, "localStorage") || !strings.Contains(kioskInit, "stageTrackerResumeContext(") {
t.Errorf("stageTrackerKioskSoundInit must resume the context and never read localStorage:\n%s", kioskInit)
}
if held := jsFunc(t, html, "stageTrackerSoundHeldNotice"); !strings.Contains(held, "if (window.HIVE_KIOSK_SOUND) return;") {
t.Errorf("stageTrackerSoundHeldNotice must be a no-op on a kiosk:\n%s", held)
}
for _, fn := range []string{"stageTrackerSetSoundOn", "stageTrackerSetVolume"} {
if body := jsFunc(t, html, fn); !strings.Contains(body, "if (!(window.hiveKioskMode && window.hiveKioskMode())) { try { localStorage.setItem(") {
t.Errorf("%s must not write localStorage in kiosk mode:\n%s", fn, body)
}
}

node, err := exec.LookPath("node")
if err != nil {
t.Skip("node not on PATH — kiosk sound default was NOT executed by this run")
}
script := strings.Join([]string{
"const assert = require('assert');",
jsFunc(t, html, "stageTrackerKioskSoundDefault"),
"assert.strictEqual(stageTrackerKioskSoundDefault(true, '?kiosk=1'), true, 'kiosk default is on');",
"assert.strictEqual(stageTrackerKioskSoundDefault(true, '?kiosk=1&sound=off'), false, 'sound=off silences');",
"assert.strictEqual(stageTrackerKioskSoundDefault(true, '?kiosk=1&sound=on'), true, 'any other value is on');",
"assert.strictEqual(stageTrackerKioskSoundDefault(true, ''), true, 'no query → on');",
"assert.strictEqual(stageTrackerKioskSoundDefault(false, '?sound=on'), false, 'never outside kiosk mode');",
}, "\n")
if out, err := exec.Command(node, "-e", script).CombinedOutput(); err != nil {
t.Fatalf("node kiosk sound default fixture failed: %v\n%s", err, out)
}
}
8 changes: 6 additions & 2 deletions src/pkg/dashboard/static-src/js/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
// monitor, suppresses the welcome/NPS dialogs and the toast hover-pause, and
// never writes to localStorage so a rolled SPA can't inherit stale state.
// The class is set here, before first paint, so the chrome never flashes.
// Tracker sounds are on by default in kiosk mode (the streamer captures them
// through a PulseAudio null sink); `&sound=off` keeps the display silent.
(function(){
var kiosk=false;
try { kiosk=new URL(window.location.href).searchParams.get('kiosk')==='1'; } catch(e) {}
var kiosk=false, sound=true;
try { kiosk=new URL(window.location.href).searchParams.get('kiosk')==='1'; sound=new URL(window.location.href).searchParams.get('sound')!=='off'; } catch(e) {}
window.HIVE_KIOSK=kiosk;
window.HIVE_KIOSK_SOUND=kiosk&&sound;
window.hiveKioskMode=function(){ return !!window.HIVE_KIOSK; };
window.hiveKioskSoundOn=function(){ return !!window.HIVE_KIOSK_SOUND; };
if(kiosk){
document.body.classList.add('kiosk-mode');
if(!location.hash) history.replaceState(null,'',location.pathname+location.search+'#section-activity-monitor-section');
Expand Down
Loading
Loading