implement inbound audio using tunnel source

This commit is contained in:
Tilman Kranz 2023-10-10 02:48:13 +02:00
parent 351a5190c9
commit 08cfa11451

View File

@ -178,30 +178,17 @@ EOF
# Check if SSH port forwarding is running
check_pa_ssh() {
if ! "$outbound" && ! "$inbound" ; then
return 0
else
for pid in $(pidof ssh) ; do
if grep -Fq $USER-pulseaudio /proc/"$pid"/cmdline ; then
if "$outbound" ; then
grep -Fq -e -L /proc/"$pid"/cmdline || {
echo "ERROR: No SSH port forwarding to remote server for outbound audio established." >&2
echo "ERROR: No SSH port forwarding to remote server is established." >&2
return 1 ;
}
fi
if "$inbound" ; then
grep -Fq -e -L /proc/"$pid"/cmdline || {
echo "ERROR: No SSH port forwarding from remote server for inbound audio established." >&2
return 1 ;
}
fi
fi
done
echo "ERROR: No SSH port forwarding established." >&2
echo "ERROR: No SSH is established." >&2
return 1
fi
}
# Perform status operation
@ -212,12 +199,12 @@ do_status() {
rv=1
fi
if "$outbound" ; then
if ! _ssh pactl list modules | grep -Fq "Name: module-native-protocol-tcp" ; then
echo "ERROR: PulseAudio module \"module-native-protocol-tcp\" is not loaded on remote_ip=$remote_ip." >&2
rv=1
fi
if "$outbound" ; then
if ! pactl list modules | grep -Fq "Name: module-tunnel-sink" ; then
echo "ERROR: PulseAudio module \"module-tunnel-sink\" is not loaded." >&2
rv=1
@ -230,13 +217,8 @@ do_status() {
fi
if "$inbound" ; then
if ! pactl list modules | grep -Fq "Name: module-native-protocol-tcp" ; then
echo "ERROR: PulseAudio module \"module-native-protocol-tcp\" is not loaded." >&2
rv=1
fi
if ! _ssh pactl list modules | grep -Fq "Name: module-tunnel-sink" ; then
echo "ERROR: PulseAudio module \"module-tunnel-sink\" is not loaded on remote_ip=$remote_ip." >&2
if ! pactl list modules | grep -Fq "Name: module-tunnel-source" ; then
echo "ERROR: PulseAudio module \"module-tunnel-source\" is not loaded." >&2
rv=1
fi
fi
@ -259,6 +241,11 @@ sync_pa_cookie() {
fi
}
# Establish SSH port forwarding to PulseAudio TCP server on remote host
establish_ssh_portforward() {
_ssh -fNT -L 127.0.0.1:4713:127.0.0.1:4713
}
# Enable PulseAudio TCP tunnel server on remote host
enable_remote_pa_tunnel_server() {
if _ssh pactl list modules | grep -Fq "Name: module-native-protocol-tcp" ; then
@ -273,31 +260,6 @@ enable_remote_pa_tunnel_server() {
fi
}
# Enable PulseAudio TCP tunnel server on local host
enable_local_pa_tunnel_server() {
if pactl list modules | grep -Fq "Name: module-native-protocol-tcp" ; then
echo "INFO: PulseAudio module \"module-native-protocol-tcp\" already loaded." >&2
return 0
elif pactl load-module module-native-protocol-tcp listen=127.0.0.1 auth-ip-acl=127.0.0.1 ; then
echo "INFO: Loaded PulseAudio module \"module-native-protocol-tcp\"." >&2
return 0
else
echo "ERROR: Unable to load PulseAudio module \"module-native-protocol-tcp\"." >&2
return 1
fi
}
# Establish SSH port forwarding to PulseAudio TCP server on remote host
establish_ssh_portforward() {
if "$outbound" && ! "$inbound" ; then
_ssh -fNT -L 127.0.0.1:4713:127.0.0.1:4713
elif "$inbound" && ! "$outbound" ; then
_ssh -fNT -R 127.0.0.1:4713:127.0.0.1:4713
elif "$outbound" && "$inbound" ; then
_ssh -fNT -L 127.0.0.1:4713:127.0.0.1:4713 -R 127.0.0.1:4713:127.0.0.1:4713
fi
}
# Enable tunnel sink on local host
enable_local_pa_tunnel_sink() {
if pactl list modules | grep -Fq "Name: module-tunnel-sink" ; then
@ -312,16 +274,16 @@ enable_local_pa_tunnel_sink() {
fi
}
# Enable tunnel sink on remote host
enable_remote_pa_tunnel_sink() {
if _ssh pactl list modules | grep -Fq "Name: module-tunnel-sink" ; then
echo "INFO: PulseAudio module \"module-tunnel-sink\" already loaded on remote_ip=$remote_ip." >&2
# Enable tunnel source on local host
enable_local_pa_tunnel_source() {
if pactl list modules | grep -Fq "Name: module-tunnel-source" ; then
echo "INFO: PulseAudio module \"module-tunnel-source\" already loaded." >&2
return 0
elif _ssh pactl load-module module-tunnel-sink server=tcp:127.0.0.1 ; then
echo "INFO: Loaded PulseAudio module \"module-tunnel-sink\" on remote_ip=$remote_ip." >&2
elif pactl load-module module-tunnel-source server=tcp:127.0.0.1 ; then
echo "INFO: Loaded PulseAudio module \"module-tunnel-source\"." >&2
return 0
else
echo "ERROR: Unable to load PulseAudio module \"module-tunnel-sink\" on remote_ip=$remote_ip." >&2
echo "ERROR: Unable to load PulseAudio module \"module-tunnel-source\"." >&2
return 1
fi
}
@ -341,45 +303,20 @@ set_local_pa_tunnel_sink_as_default() {
do_start() {
sync_pa_cookie || return 1
establish_ssh_portforward || return 1
enable_remote_pa_tunnel_server || return 1
if "$outbound" ; then
enable_remote_pa_tunnel_server || return 1
enable_local_pa_tunnel_sink || return 1
set_local_pa_tunnel_sink_as_default || return 1
fi
if "$inbound" ; then
enable_local_pa_tunnel_server || return 1
enable_remote_pa_tunnel_sink || return 1
enable_local_pa_tunnel_source || return 1
fi
return 0
}
# Remove PulseAudio TCP tunnel sink on remote host
remove_remote_pa_tunnel_sink() {
if ! _ssh pactl list modules | grep -Fq "Name: module-tunnel-sink" ; then
echo "INFO: PulseAudio module \"module-tunnel-sink\" is not loaded on remote_ip=$remote_ip." >&2
return 0
elif ! _ssh pactl list sinks | grep -Fq "tunnel-sink.tcp:127.0.0.1" ; then
echo "INFO: No PulseAudio tunnel sink to 127.0.0.1 exists on remote_ip=$remote_ip." >&2
return 0
else
owner_module=$(
_ssh pactl --format json list sinks 2>/dev/null | \
jq '.[] | select(.name=="tunnel-sink.tcp:127.0.0.1") | .owner_module' -r
)
if ! _ssh pactl unload-module "$owner_module" ; then
echo "ERROR: Could not unload owner module $owner_module of PulseAudio sink \"tunnel-sink.tcp:127.0.0.1\" on remote_ip=$remote_ip." >&2
return 1
else
echo "INFO: Unloaded owner module $owner_module of PulseAudio sink \"tunnel-sink.tcp:127.0.0.1\" on remote_ip=$remote_ip." >&2
return 0
fi
fi
}
# Remove PulseAudio TCP tunnel sink on local host
remove_local_pa_tunnel_sink() {
if ! pactl list modules | grep -Fq "Name: module-tunnel-sink" ; then
@ -404,18 +341,28 @@ remove_local_pa_tunnel_sink() {
fi
}
# Stop PulseAudio TCP tunnel server on local host.
disable_local_pa_tunnel_server() {
if ! pactl list modules | grep -Fq "Name: module-native-protocol-tcp" ; then
echo "INFO: PulseAudio module \"module-native-protocol-tcp\" not loaded." >&2
# Remove PulseAudio TCP tunnel source on local host
remove_local_pa_tunnel_source() {
if ! pactl list modules | grep -Fq "Name: module-tunnel-source" ; then
echo "INFO: PulseAudio module \"module-tunnel-source\" is not loaded." >&2
return 0
elif ! pactl unload-module module-native-protocol-tcp ; then
echo "ERROR: Could not unload PulseAudio module \"module-native-protocol-tcp\"." >&2
elif ! pactl list sources | grep -Fq "tunnel-source.tcp:127.0.0.1" ; then
echo "INFO: No PulseAudio tunnel source from 127.0.0.1 exists." >&2
return 0
else
owner_module=$(
pactl --format json list sources 2>/dev/null | \
jq '.[] | select(.name=="tunnel-source.tcp:127.0.0.1") | .owner_module' -r
)
if ! pactl unload-module "$owner_module" ; then
echo "ERROR: Could not unload owner module $owner_module of PulseAudio source \"tunnel-source.tcp:127.0.0.1\"." >&2
return 1
else
echo "INFO: Unloaded PulseAudio module \"module-native-protocol-tcp\"." >&2
echo "INFO: Unloaded owner module $owner_module of PulseAudio source \"tunnel-source.tcp:127.0.0.1\"." >&2
return 0
fi
fi
}
# Stop PulseAudio TCP tunnel server on remote host.
@ -445,14 +392,13 @@ terminate_ssh_portforward() {
do_stop() {
if "$outbound" ; then
remove_local_pa_tunnel_sink || return 1
disable_remote_pa_tunnel_server || return 1
fi
if "$inbound" ; then
remove_remote_pa_tunnel_sink || return 1
disable_local_pa_tunnel_server || return 1
remove_local_pa_tunnel_source || return 1
fi
disable_remote_pa_tunnel_server || return 1
terminate_ssh_portforward || return 1
return 0