2021-03-01 01:40:41 +00:00
|
|
|
#!/bin/bash -e
|
2021-02-28 17:04:41 +00:00
|
|
|
|
2025-04-20 14:19:43 +00:00
|
|
|
#target_url="${1:-icecast://source:source@127.0.0.1:61120/main}"
|
|
|
|
: "${TARGET_IP:=127.0.0.1}"
|
|
|
|
: "${TARGET_PORT:=61120}"
|
|
|
|
: "${TARGET_MOUNT:=/main}"
|
|
|
|
: "${TARGET_USERNAME:=source}"
|
|
|
|
: "${TARGET_PASSWORD:=source}"
|
|
|
|
: "${NDI_FEEDER_EXTRA_IP:=}"
|
2021-02-28 17:04:41 +00:00
|
|
|
|
2025-04-20 14:19:43 +00:00
|
|
|
gstreamer_pids=()
|
|
|
|
|
|
|
|
call_gstreamer() {
|
|
|
|
command gst-launch-1.0 "$@"
|
2021-02-28 17:04:41 +00:00
|
|
|
}
|
|
|
|
|
2025-04-20 14:19:43 +00:00
|
|
|
daemon_gstreamer() {
|
|
|
|
call_gstreamer "$@" &
|
|
|
|
gstreamer_pids+=($!)
|
2021-02-28 17:04:41 +00:00
|
|
|
}
|
|
|
|
|
2025-04-20 14:19:43 +00:00
|
|
|
shutdown_gstreamer() {
|
|
|
|
if is_gstreamer_running; then
|
|
|
|
kill "$gstreamer_pid" || true
|
2022-03-08 07:56:59 +00:00
|
|
|
for t in $(seq 0 10); do
|
2025-04-20 14:19:43 +00:00
|
|
|
if ! kill -0 "$gstreamer_pid"; then
|
2021-10-12 18:32:57 +00:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
sleep 1
|
|
|
|
done
|
2025-04-20 14:19:43 +00:00
|
|
|
if kill -0 "$gstreamer_pid"; then
|
|
|
|
kill -9 "$gstreamer_pid" || true
|
2021-10-12 18:32:57 +00:00
|
|
|
fi
|
2021-02-28 17:04:41 +00:00
|
|
|
fi
|
2025-04-20 14:19:43 +00:00
|
|
|
gstreamer_pid=
|
2021-02-28 17:04:41 +00:00
|
|
|
}
|
|
|
|
|
2025-04-20 14:19:43 +00:00
|
|
|
is_gstreamer_running() {
|
|
|
|
[ -n "$gstreamer_pid" ] && kill -0 "$gstreamer_pid"
|
2021-02-28 17:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
on_exit() {
|
2025-04-20 14:19:43 +00:00
|
|
|
shutdown_gstreamer
|
2021-02-28 17:04:41 +00:00
|
|
|
}
|
|
|
|
trap on_exit EXIT
|
|
|
|
|
2021-03-01 00:51:10 +00:00
|
|
|
offline=0
|
|
|
|
|
2025-04-20 14:19:43 +00:00
|
|
|
url_address=()
|
|
|
|
if [ -n "$NDI_FEEDER_EXTRA_IP" ]; then
|
|
|
|
url_address=("url-address=$NDI_FEEDER_EXTRA_IP:5961")
|
|
|
|
fi
|
2021-02-28 17:04:41 +00:00
|
|
|
|
2025-04-20 14:19:43 +00:00
|
|
|
while true; do
|
|
|
|
found_audio_source="$(grep --line-buffered -m 1 --color=none -Po 'ndi-name = \K.+\(ID.* Main Audio.*\)$' < <(gst-device-monitor-1.0 -f Source/Network:application/x-ndi))"
|
2021-02-28 17:04:41 +00:00
|
|
|
|
2022-03-08 07:56:59 +00:00
|
|
|
if [ -z "$found_audio_source" ]; then
|
|
|
|
offline=$((offline + 1))
|
2021-03-01 00:51:10 +00:00
|
|
|
else
|
|
|
|
offline=0
|
|
|
|
fi
|
|
|
|
|
2025-04-20 14:19:43 +00:00
|
|
|
if ! is_gstreamer_running && [ -n "$found_audio_source" ]; then
|
|
|
|
echo "starting gstreamer with audio source: $found_audio_source" >&2
|
2022-03-08 07:56:59 +00:00
|
|
|
|
2025-04-20 14:19:43 +00:00
|
|
|
call_gstreamer ndisrc ndi-name="$found_audio_source" "${url_address[@]}" ! ndisrcdemux name=demux \
|
|
|
|
demux.audio ! queue ! audioconvert ! audio/x-raw, channels=2, rate=48000, format=S16LE ! filesink location=/dev/stdout |
|
2022-03-09 11:17:08 +00:00
|
|
|
fakesilence --samplerate 48000 --channels 2 --silence-threshold 125ms |
|
2025-04-20 14:19:43 +00:00
|
|
|
daemon_gstreamer filesrc location=/dev/stdin ! rawaudioparse use-sink-caps=false format=pcm pcm-format=s16le sample-rate=48000 num-channels=2 ! queue ! audioconvert ! audioresample ! flacenc ! oggmux ! shout2send mount="$TARGET_MOUNT" port="$TARGET_PORT" username="$TARGET_USERNAME" password="$TARGET_PASSWORD" ip="$TARGET_IP"
|
|
|
|
elif is_gstreamer_running && [ -z "$found_audio_source" ] && [ "$offline" -gt 0 ]; then
|
|
|
|
echo "shutting down gstreamer since no source has been found" >&2
|
|
|
|
shutdown_gstreamer # it won't shut down by itself unfortunately
|
2021-02-28 17:04:41 +00:00
|
|
|
fi
|
2022-03-08 07:56:59 +00:00
|
|
|
|
2021-03-07 21:21:57 +00:00
|
|
|
sleep 1
|
2021-02-28 17:04:41 +00:00
|
|
|
done
|