Files
Phoenix/session.sh
T
2026-06-02 20:56:19 +02:00

165 lines
6.4 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# ==========================================
# Analyzer Kiosk Session for Raspberry Pi OS
#
# This script launches Phoenix and the audio analyser UI in kiosk mode
# using Chromium. It assumes that a lightweight HTTP server is running
# on http://localhost to serve the analyser files. The X session
# is started by the accompanying systemd unit. Phoenix owns the audio
# hardware; Chromium only renders the GUI.
# ==========================================
set -euo pipefail
# Resolve the installation root from the actual script location so the
# setup does not depend on a hard-coded home directory.
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
# ----- Logging -----
# Write logs to the kiosk user's home directory. Use an
# environment variable to allow overrides.
LOG="${LOG:-/home/analyzer/analyzer-kiosk.log}"
KIOSK_USER="${KIOSK_USER:-analyzer}"
CHROME_USER_DATA_DIR="${CHROME_USER_DATA_DIR:-/data/chrome-analyzer}"
PHOENIX_DIR="${PHOENIX_DIR:-$SCRIPT_DIR}"
PHOENIX_BIN="${PHOENIX_BIN:-$PHOENIX_DIR/target/release/phoenix}"
PHOENIX_LOG_FILE="${PHOENIX_LOG_FILE:-/home/analyzer/phoenix.log}"
# Listen on all interfaces so Phoenix is reachable both locally on the Pi
# and from external browsers in the LAN.
PHOENIX_LISTEN="${PHOENIX_LISTEN:-0.0.0.0:8789}"
PHOENIX_ALSA_DEVICE="${PHOENIX_ALSA_DEVICE:-hw:CARD=CODEC,DEV=0}"
PHOENIX_SAMPLE_RATE="${PHOENIX_SAMPLE_RATE:-48000}"
PHOENIX_PERIOD_SIZE="${PHOENIX_PERIOD_SIZE:-128}"
PHOENIX_BUFFER_SIZE="${PHOENIX_BUFFER_SIZE:-512}"
PHOENIX_GLOBAL_CONFIG_PATH="${PHOENIX_GLOBAL_CONFIG_PATH:-/home/analyzer/.config/phoenix/global-config.json}"
PHOENIX_RECORDINGS_DIR_ANALYZER="${PHOENIX_RECORDINGS_DIR_ANALYZER:-/home/analyzer/Aufnahmen}"
PHOENIX_RECORDINGS_DIR_VOLUMIO="${PHOENIX_RECORDINGS_DIR_VOLUMIO:-/home/volumio/Aufnahmen}"
PHOENIX_LR_FRAC_DELAY_ENABLED="${PHOENIX_LR_FRAC_DELAY_ENABLED:-true}"
PHOENIX_LR_FRAC_DELAY_SAMPLES="${PHOENIX_LR_FRAC_DELAY_SAMPLES:-0.996}"
mkdir -p "$(dirname "$LOG")"
exec >>"$LOG" 2>&1
echo "=== $(date): session.sh START ==="
configure_locale() {
if locale -a 2>/dev/null | grep -Eiq '^de_DE\.(utf8|UTF-8)$'; then
export LANG=de_DE.UTF-8
export LC_ALL=de_DE.UTF-8
elif locale -a 2>/dev/null | grep -Eiq '^C\.UTF-?8$'; then
export LANG=C.UTF-8
export LC_ALL=C.UTF-8
else
export LANG=C
export LC_ALL=C
fi
}
# ----- Environment -----
export DISPLAY=:0
configure_locale
if [ ! -x "$PHOENIX_BIN" ] || [ ! -s "$PHOENIX_BIN" ]; then
echo "Phoenix binary missing, empty or not executable: $PHOENIX_BIN"
exit 1
fi
if pgrep -f "$PHOENIX_BIN" >/dev/null 2>&1; then
echo "Phoenix already running; reusing existing process."
else
echo "Starting Phoenix: $PHOENIX_BIN"
if [ "$(id -u)" -eq 0 ]; then
install -d -o analyzer -g analyzer -m 0755 "$PHOENIX_RECORDINGS_DIR_ANALYZER" || true
install -d -o volumio -g volumio -m 0755 "$PHOENIX_RECORDINGS_DIR_VOLUMIO" || true
else
mkdir -p "$PHOENIX_RECORDINGS_DIR_ANALYZER" || true
mkdir -p "$PHOENIX_RECORDINGS_DIR_VOLUMIO" || true
fi
if [ "$(id -u)" -eq 0 ]; then
install -o "$KIOSK_USER" -g "$KIOSK_USER" -m 0644 /dev/null "$PHOENIX_LOG_FILE" 2>/dev/null || true
sudo -u "$KIOSK_USER" env \
PHOENIX_LISTEN="$PHOENIX_LISTEN" \
PHOENIX_ALSA_DEVICE="$PHOENIX_ALSA_DEVICE" \
PHOENIX_SAMPLE_RATE="$PHOENIX_SAMPLE_RATE" \
PHOENIX_PERIOD_SIZE="$PHOENIX_PERIOD_SIZE" \
PHOENIX_BUFFER_SIZE="$PHOENIX_BUFFER_SIZE" \
PHOENIX_GLOBAL_CONFIG_PATH="$PHOENIX_GLOBAL_CONFIG_PATH" \
PHOENIX_RECORDINGS_DIR_ANALYZER="$PHOENIX_RECORDINGS_DIR_ANALYZER" \
PHOENIX_RECORDINGS_DIR_VOLUMIO="$PHOENIX_RECORDINGS_DIR_VOLUMIO" \
PHOENIX_LR_FRAC_DELAY_ENABLED="$PHOENIX_LR_FRAC_DELAY_ENABLED" \
PHOENIX_LR_FRAC_DELAY_SAMPLES="$PHOENIX_LR_FRAC_DELAY_SAMPLES" \
"$PHOENIX_BIN" >>"$PHOENIX_LOG_FILE" 2>&1 &
else
touch "$PHOENIX_LOG_FILE"
env \
PHOENIX_LISTEN="$PHOENIX_LISTEN" \
PHOENIX_ALSA_DEVICE="$PHOENIX_ALSA_DEVICE" \
PHOENIX_SAMPLE_RATE="$PHOENIX_SAMPLE_RATE" \
PHOENIX_PERIOD_SIZE="$PHOENIX_PERIOD_SIZE" \
PHOENIX_BUFFER_SIZE="$PHOENIX_BUFFER_SIZE" \
PHOENIX_GLOBAL_CONFIG_PATH="$PHOENIX_GLOBAL_CONFIG_PATH" \
PHOENIX_RECORDINGS_DIR_ANALYZER="$PHOENIX_RECORDINGS_DIR_ANALYZER" \
PHOENIX_RECORDINGS_DIR_VOLUMIO="$PHOENIX_RECORDINGS_DIR_VOLUMIO" \
PHOENIX_LR_FRAC_DELAY_ENABLED="$PHOENIX_LR_FRAC_DELAY_ENABLED" \
PHOENIX_LR_FRAC_DELAY_SAMPLES="$PHOENIX_LR_FRAC_DELAY_SAMPLES" \
"$PHOENIX_BIN" >>"$PHOENIX_LOG_FILE" 2>&1 &
fi
fi
PHOENIX_HEALTH_TARGET="$PHOENIX_LISTEN"
case "$PHOENIX_HEALTH_TARGET" in
0.0.0.0:*) PHOENIX_HEALTH_TARGET="127.0.0.1:${PHOENIX_HEALTH_TARGET#*:}" ;;
:::*) PHOENIX_HEALTH_TARGET="[::1]:${PHOENIX_HEALTH_TARGET#*:}" ;;
esac
PHOENIX_HEALTH_URL="http://${PHOENIX_HEALTH_TARGET}/health"
if command -v curl >/dev/null 2>&1; then
echo "Waiting for Phoenix health: $PHOENIX_HEALTH_URL"
for _ in $(seq 1 40); do
if curl -fsS "$PHOENIX_HEALTH_URL" >/dev/null 2>&1; then
echo "Phoenix is ready."
break
fi
sleep 0.25
done
else
sleep 2
fi
# ----- Start the kiosk browser -----
# Disable screen blanking and power management if xset is available.
if command -v xset >/dev/null 2>&1; then
xset -dpms || true
xset s off || true
xset s noblank || true
fi
# Ensure the Chromium profile directory exists with stable ownership,
# otherwise Chromium may fall back to a profile under the user's home.
if [ "$(id -u)" -eq 0 ]; then
install -d -m 0755 /data
install -d -o "$KIOSK_USER" -g "$KIOSK_USER" -m 0700 "$CHROME_USER_DATA_DIR"
else
mkdir -p "$CHROME_USER_DATA_DIR"
chmod 700 "$CHROME_USER_DATA_DIR"
fi
# Launch Chromium as the non-root kiosk user. The analyser UI is served
# over HTTP on localhost. Audio capture is handled exclusively by
# Phoenix, so Chromium only renders the GUI.
sudo -u "$KIOSK_USER" /usr/bin/chromium \
--kiosk \
--touch-events \
--user-data-dir="$CHROME_USER_DATA_DIR" \
--load-extension='/data/VirtualKeyboard' \
--no-first-run --no-default-browser-check \
--disable-session-crashed-bubble --disable-restore-session-state --restore-last-session=false \
--disable-background-networking --disable-remote-extensions \
--disable-pinch \
--force-device-scale-factor=1 \
--lang=de-DE --disable-translate --disable-features=Translate,TranslateUI \
--memory-pressure-off \
--max-active-webgl-contexts=1 \
http://localhost/index.html \
>>"$LOG" 2>&1
echo "=== $(date): session.sh END ==="