add pulseaudio socket bridge

This commit is contained in:
Izalia Mae 2021-07-28 16:21:54 -04:00
parent 82ac51ec10
commit d3b061a342

73
pa-socket-bridge.py Normal file
View file

@ -0,0 +1,73 @@
# Pulseaudio Socket Bridge v1.0
# by Zoey Mae (izalia@barkshark.xyz)
# Creates a socket to listen on and sends any connections to the specified
# Pulseaudio server. Requires socat which should be in your distro's repos.
# Socat will try to reconnect and will be restarted if closed for any reason
# Usage:
# pa-socket-bridge.py [server ip]
import os, signal, sys, time
from pathlib import Path
from subprocess import Popen
try:
server = sys.argv[1]
except IndexError:
server = "127.0.0.1"
sock_path = Path(f'/var/run/user/{os.getuid()}/pulse/native')
command = ['socat', f'UNIX-LISTEN:{sock_path},fork,reuseaddr,mode=777', f'TCP:{server}:4713,retry']
if not sock_path.parent.exists():
sock_path.mkdir(parents=True)
if sock_path.exists():
print('Warning: Socket exists. Is Pulseaudio running?')
def signal_handler(func, *args, **kwargs):
handler = lambda signum, frame: func(signum, frame, *args, **kwargs)
signal.signal(signal.SIGHUP, handler)
signal.signal(signal.SIGINT, handler)
signal.signal(signal.SIGQUIT, handler)
signal.signal(signal.SIGTERM, handler)
def handle_shutdown(signum, frame):
global shutdown
shutdown = True
if __name__ == '__main__':
print('Starting Pulseaudio socket bridge')
proc = Popen(command)
shutdown = False
timer = 0.0
signal_handler(handle_shutdown)
while not shutdown:
if proc.poll() != None:
print('Starting Pulseaudio socket bridge back up')
proc = Popen(command)
time.sleep(0.25)
print('Terminating process')
proc.terminate()
while proc.poll() == None:
time.sleep(0.25)
timer += 0.25
if timer >= 5.0:
proc.kill()
print('Process failed to terminate. Killing process.')
break
print('Bye! :3')