Skip to content

Instantly share code, notes, and snippets.

@shebpamm
Created August 3, 2022 11:04
Show Gist options
  • Save shebpamm/3cd5280989e87f99148cc872af37164f to your computer and use it in GitHub Desktop.
Save shebpamm/3cd5280989e87f99148cc872af37164f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
shopt -s nullglob
_save_module_id () {
printf "%s" "$1" > "$PROCDIR/$2/$3"
}
_remove_module_id() {
grep -Rl "$1" "$PROCDIR/$2" | xargs rm
}
_module_present() {
FPATH="$PROCDIR/$1/$2"
if [ -f "$FPATH" ]; then
return 0
else
return 1
fi
}
_load_module () {
module_escaped="$(echo "$@" | sed 's/[^A-Za-z0-9._-]/_/g')"
if ! _module_present local "$module_escaped"; then
_save_module_id "$(pactl load-module "$@")" local "$module_escaped"
else
echo "Module '$*' is already loaded"
fi
}
_unload_module() {
pactl unload-module "$1"
_remove_module_id "$1" local
}
_load_remote_module () {
module_escaped="$(echo "$@" | sed 's/[^A-Za-z0-9._-]/_/g')"
if ! _module_present remote "$module_escaped"; then
_save_module_id "$(ssh "$REMOTE_HOST" pactl load-module "$@")" remote "$module_escaped"
else
echo "Remote module '$*' is already loaded"
fi
}
_unload_remote_module() {
ssh "$REMOTE_HOST" pactl unload-module "$1"
_remove_module_id "$1" remote
}
_start() {
_load_module module-native-protocol-tcp
_load_remote_module module-native-protocol-tcp
_load_remote_module module-tunnel-sink server=tcp:192.168.1.234
_load_remote_module module-null-sink sink_name=mic_forward
# Set mic_forward as the default sink
ssh "$REMOTE_HOST" pactl set-default-sink mic_forward
_load_module module-tunnel-sink server=tcp:192.168.11.201
_load_module module-loopback sink=tunnel-sink.tcp:192.168.11.201
# Find local ip that will be used for ssh
local_ip=$(ip route get "$(echo "$REMOTE_HOST"| sed 's/^.*@//g')" | awk '{ for (x=1;x<=NF;x++) if ($x ~ /src/) print $(x+1)}' | tr -d '[:space:]')
# Set tunnel as the default sink
ssh "$REMOTE_HOST" pactl set-default-sink "tunnel-sink.tcp:$local_ip"
# Set default source to mic_forward.monitor
ssh "$REMOTE_HOST" pactl set-default-source mic_forward.monitor
_load_remote_module module-remap-source master=mic_forward.monitor source_name=remote_mic
}
_stop() {
for f in "$PROCDIR"/local/*; do
_unload_module "$(cat "$f")"
done
for f in "$PROCDIR"/remote/*; do
_unload_remote_module "$(cat "$f")"
done
}
# No command given
if [[ -z $1 ]]; then
echo "$(basename "$0") start/stop <ip>"
exit 1
fi
COMMAND="$1"
shift
if [[ -z $1 ]]; then
echo no host.
exit 1
fi
REMOTE_HOST="$1"
PROCDIR="/var/run/user/$UID/forwaudio"
mkdir -p $PROCDIR/{local,remote}
if [[ $COMMAND == "start" ]]; then
_start
fi
if [[ $COMMAND == "stop" ]]; then
_stop
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment