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 if SSH port forwarding is running
check_pa_ssh() { check_pa_ssh() {
if ! "$outbound" && ! "$inbound" ; then
return 0
else
for pid in $(pidof ssh) ; do for pid in $(pidof ssh) ; do
if grep -Fq $USER-pulseaudio /proc/"$pid"/cmdline ; then if grep -Fq $USER-pulseaudio /proc/"$pid"/cmdline ; then
if "$outbound" ; then
grep -Fq -e -L /proc/"$pid"/cmdline || { 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 ; return 1 ;
} }
fi 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 done
echo "ERROR: No SSH port forwarding established." >&2 echo "ERROR: No SSH is established." >&2
return 1 return 1
fi
} }
# Perform status operation # Perform status operation
@ -212,12 +199,12 @@ do_status() {
rv=1 rv=1
fi fi
if "$outbound" ; then
if ! _ssh pactl list modules | grep -Fq "Name: module-native-protocol-tcp" ; 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 echo "ERROR: PulseAudio module \"module-native-protocol-tcp\" is not loaded on remote_ip=$remote_ip." >&2
rv=1 rv=1
fi fi
if "$outbound" ; then
if ! pactl list modules | grep -Fq "Name: module-tunnel-sink" ; then if ! pactl list modules | grep -Fq "Name: module-tunnel-sink" ; then
echo "ERROR: PulseAudio module \"module-tunnel-sink\" is not loaded." >&2 echo "ERROR: PulseAudio module \"module-tunnel-sink\" is not loaded." >&2
rv=1 rv=1
@ -230,13 +217,8 @@ do_status() {
fi fi
if "$inbound" ; then if "$inbound" ; then
if ! pactl list modules | grep -Fq "Name: module-native-protocol-tcp" ; then if ! pactl list modules | grep -Fq "Name: module-tunnel-source" ; then
echo "ERROR: PulseAudio module \"module-native-protocol-tcp\" is not loaded." >&2 echo "ERROR: PulseAudio module \"module-tunnel-source\" 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
rv=1 rv=1
fi fi
fi fi
@ -259,6 +241,11 @@ sync_pa_cookie() {
fi 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 PulseAudio TCP tunnel server on remote host
enable_remote_pa_tunnel_server() { enable_remote_pa_tunnel_server() {
if _ssh pactl list modules | grep -Fq "Name: module-native-protocol-tcp" ; then if _ssh pactl list modules | grep -Fq "Name: module-native-protocol-tcp" ; then
@ -273,31 +260,6 @@ enable_remote_pa_tunnel_server() {
fi 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 tunnel sink on local host
enable_local_pa_tunnel_sink() { enable_local_pa_tunnel_sink() {
if pactl list modules | grep -Fq "Name: module-tunnel-sink" ; then if pactl list modules | grep -Fq "Name: module-tunnel-sink" ; then
@ -312,16 +274,16 @@ enable_local_pa_tunnel_sink() {
fi fi
} }
# Enable tunnel sink on remote host # Enable tunnel source on local host
enable_remote_pa_tunnel_sink() { enable_local_pa_tunnel_source() {
if _ssh pactl list modules | grep -Fq "Name: module-tunnel-sink" ; then if pactl list modules | grep -Fq "Name: module-tunnel-source" ; then
echo "INFO: PulseAudio module \"module-tunnel-sink\" already loaded on remote_ip=$remote_ip." >&2 echo "INFO: PulseAudio module \"module-tunnel-source\" already loaded." >&2
return 0 return 0
elif _ssh pactl load-module module-tunnel-sink server=tcp:127.0.0.1 ; then elif pactl load-module module-tunnel-source server=tcp:127.0.0.1 ; then
echo "INFO: Loaded PulseAudio module \"module-tunnel-sink\" on remote_ip=$remote_ip." >&2 echo "INFO: Loaded PulseAudio module \"module-tunnel-source\"." >&2
return 0 return 0
else 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 return 1
fi fi
} }
@ -341,45 +303,20 @@ set_local_pa_tunnel_sink_as_default() {
do_start() { do_start() {
sync_pa_cookie || return 1 sync_pa_cookie || return 1
establish_ssh_portforward || return 1 establish_ssh_portforward || return 1
enable_remote_pa_tunnel_server || return 1
if "$outbound" ; then if "$outbound" ; then
enable_remote_pa_tunnel_server || return 1
enable_local_pa_tunnel_sink || return 1 enable_local_pa_tunnel_sink || return 1
set_local_pa_tunnel_sink_as_default || return 1 set_local_pa_tunnel_sink_as_default || return 1
fi fi
if "$inbound" ; then if "$inbound" ; then
enable_local_pa_tunnel_server || return 1 enable_local_pa_tunnel_source || return 1
enable_remote_pa_tunnel_sink || return 1
fi fi
return 0 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 PulseAudio TCP tunnel sink on local host
remove_local_pa_tunnel_sink() { remove_local_pa_tunnel_sink() {
if ! pactl list modules | grep -Fq "Name: module-tunnel-sink" ; then if ! pactl list modules | grep -Fq "Name: module-tunnel-sink" ; then
@ -404,18 +341,28 @@ remove_local_pa_tunnel_sink() {
fi fi
} }
# Stop PulseAudio TCP tunnel server on local host. # Remove PulseAudio TCP tunnel source on local host
disable_local_pa_tunnel_server() { remove_local_pa_tunnel_source() {
if ! pactl list modules | grep -Fq "Name: module-native-protocol-tcp" ; then if ! pactl list modules | grep -Fq "Name: module-tunnel-source" ; then
echo "INFO: PulseAudio module \"module-native-protocol-tcp\" not loaded." >&2 echo "INFO: PulseAudio module \"module-tunnel-source\" is not loaded." >&2
return 0 return 0
elif ! pactl unload-module module-native-protocol-tcp ; then elif ! pactl list sources | grep -Fq "tunnel-source.tcp:127.0.0.1" ; then
echo "ERROR: Could not unload PulseAudio module \"module-native-protocol-tcp\"." >&2 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 return 1
else 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 return 0
fi fi
fi
} }
# Stop PulseAudio TCP tunnel server on remote host. # Stop PulseAudio TCP tunnel server on remote host.
@ -445,14 +392,13 @@ terminate_ssh_portforward() {
do_stop() { do_stop() {
if "$outbound" ; then if "$outbound" ; then
remove_local_pa_tunnel_sink || return 1 remove_local_pa_tunnel_sink || return 1
disable_remote_pa_tunnel_server || return 1
fi fi
if "$inbound" ; then if "$inbound" ; then
remove_remote_pa_tunnel_sink || return 1 remove_local_pa_tunnel_source || return 1
disable_local_pa_tunnel_server || return 1
fi fi
disable_remote_pa_tunnel_server || return 1
terminate_ssh_portforward || return 1 terminate_ssh_portforward || return 1
return 0 return 0