Repository navigation
Expand file tree
/
Copy pathentrypoint.sh
More file actions
130 lines (100 loc) · 3.71 KB
/
Copy pathentrypoint.sh
File metadata and controls
130 lines (100 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env bash
set -eo pipefail
# 0. Pre-flight Hardware Access Check
if [ ! -d /dev/snd ]; then
echo "WARNING: /dev/snd directory not found!"
echo "Ensure you pass sound hardware into Docker using '--device /dev/snd' or Compose 'devices:' mapping."
fi
# 1. Directory Structure Setup
mkdir -p /tmp /run/mpd /var/lib/mpd /music/cache /data /data/cache /data/playlists /data/golibrespot /data/snapserver /var/log/audio-services
# Copy default golibrespot config ONLY if a persistent config does not already exist
if [ ! -f /data/golibrespot/config.yml ] && [ -f /etc/golibrespot/config.yml ]; then
echo "Initializing default Golibrespot configuration..."
envsubst < /etc/golibrespot/config.yml > /data/golibrespot/config.yml
fi
# 2. FIFO Pipe Cleanup & Initialization
rm -f /tmp/snapfifo /tmp/mpd_socket.sock
mkfifo /tmp/snapfifo
mkfifo /tmp/mpd_socket.sock
chmod 666 /tmp/snapfifo /tmp/mpd_socket.sock
# 3. Graceful Termination Handler
cleanup() {
echo "Termination signal received. Gracefully shutting down audio services..."
touch /tmp/shutting_down
echo "Stopping .NET Web API..."
kill -TERM "$DOTNET_PID" 2>/dev/null || true
wait "$DOTNET_PID" 2>/dev/null || true
echo "Stopping Snapclient..."
kill -TERM "$SNAPCLIENT_PID" 2>/dev/null || true
wait "$SNAPCLIENT_PID" 2>/dev/null || true
echo "Stopping MPD..."
kill -TERM "$MPD_PID" 2>/dev/null || true
wait "$MPD_PID" 2>/dev/null || true
echo "Stopping Snapserver..."
kill -TERM "$SNAPSERVER_PID" 2>/dev/null || true
wait "$SNAPSERVER_PID" 2>/dev/null || true
echo "Stopping Librespot..."
kill -TERM "$LIBRESPOT_PID" 2>/dev/null || true
wait "$LIBRESPOT_PID" 2>/dev/null || true
echo "All processes stopped cleanly."
exit 0
}
trap cleanup SIGTERM SIGINT
# 4. Start Services
echo "Starting Snapserver..."
envsubst < /etc/snapserver.conf.template > /etc/snapserver.conf
snapserver --config /etc/snapserver.conf &
SNAPSERVER_PID=$!
echo "Starting go-librespot..."
go-librespot --config_dir /data/golibrespot/ &
LIBRESPOT_PID=$!
sleep 1
curl -X POST "http://127.0.0.1:3678/player/volume" \
-H "Content-Type: application/json" \
-d "{\"volume\": ${GOLIBRESPOT_INPUT_VOLUME}}"
echo "Starting Snapclient (Targeting ALSA 'hardware' device)..."
# Critical: Use -s hardware to prevent audio loopback through alsaequal/snapfifo
snapclient --player alsa \
-s "hardware" \
--hostID "${DEVICE_NAME}" \
--sampleformat "44100:16:*" \
--latency 30 \
tcp://127.0.0.1:1704 &
SNAPCLIENT_PID=$!
echo "Starting MPD..."
# Run MPD in no-daemon mode so we capture its PID cleanly for health tracking
envsubst < /etc/mpd.conf.template > /etc/mpd.conf
mpd --no-daemon &
MPD_PID=$!
# Wait briefly for MPD socket initialization before triggering mpc
sleep 1
mpc volume $MPD_INPUT_VOLUME 2>/dev/null || true
mpc update 2>/dev/null || true
echo "Starting .NET 10 Web API..."
dotnet Viox.Server.dll &
DOTNET_PID=$!
# 5. Process Health Monitor Loop
echo "All audio services started. Monitoring process health..."
while true; do
if ! kill -0 "$SNAPSERVER_PID" 2>/dev/null; then
echo "CRITICAL: Snapserver process died unexpectedly."
cleanup
fi
if ! kill -0 "$LIBRESPOT_PID" 2>/dev/null; then
echo "CRITICAL: go-librespot process died unexpectedly."
cleanup
fi
if ! kill -0 "$SNAPCLIENT_PID" 2>/dev/null; then
echo "CRITICAL: Snapclient process died unexpectedly."
cleanup
fi
if ! kill -0 "$MPD_PID" 2>/dev/null; then
echo "CRITICAL: MPD process died unexpectedly."
cleanup
fi
if ! kill -0 "$DOTNET_PID" 2>/dev/null; then
echo "CRITICAL: .NET Web API process died unexpectedly."
cleanup
fi
sleep 3
done