pa-socket-bridge: exit on existing socket and update description

This commit is contained in:
Izalia Mae 2021-07-28 17:01:19 -04:00
parent d3b061a342
commit d2a6012ae7

View file

@ -1,10 +1,10 @@
# Pulseaudio Socket Bridge v1.0
# Pulseaudio Socket Bridge v1.0.1
# 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
#
# Creates a unix socket to listen on and sends any connections to the specified
# Pulseaudio server via socat. Socat will try to reconnect and will be restarted
# if closed for any reason
#
# Usage:
# pa-socket-bridge.py [server ip]
@ -20,12 +20,14 @@ except IndexError:
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']
shutdown = False
if not sock_path.parent.exists():
sock_path.mkdir(parents=True)
if sock_path.exists():
print('Warning: Socket exists. Is Pulseaudio running?')
print('Socket already exists. Is Pulseaudio or another bridge running?')
sys.exit()
def signal_handler(func, *args, **kwargs):
@ -44,15 +46,15 @@ def handle_shutdown(signum, frame):
if __name__ == '__main__':
print('Starting Pulseaudio socket bridge')
proc = Popen(command)
shutdown = False
timer = 0.0
signal_handler(handle_shutdown)
proc = Popen(command)
timer = 0.0
while not shutdown:
if proc.poll() != None:
print('Starting Pulseaudio socket bridge back up')
print('Restarting process')
proc = Popen(command)
time.sleep(0.25)
@ -65,9 +67,8 @@ if __name__ == '__main__':
timer += 0.25
if timer >= 5.0:
proc.kill()
print('Process failed to terminate. Killing process.')
print('Socat failed to terminate. Killing process.')
break
print('Bye! :3')