From 4eca56929f729eb402275b0879496ee3aa8ef22c Mon Sep 17 00:00:00 2001 From: Edward Brown Date: Sat, 4 Nov 2023 15:54:52 +0000 Subject: [PATCH] Support sharing Docker audio with host --- Dockerfile | 7 ++++++- amniotic.client.conf | 6 ++++++ amniotic/mqtt/loop.py | 17 +++++++++++------ amniotic/mqtt/start.py | 22 ++++++++++++++++++++++ setup.py | 2 +- 5 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 amniotic.client.conf create mode 100644 amniotic/mqtt/start.py diff --git a/Dockerfile b/Dockerfile index e41216e..343e6bb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,8 +3,13 @@ ARG TYPE=development FROM edwardbrown/python as base ENV DEBIAN_FRONTEND=noninteractive + +RUN useradd --uid 1000 --create-home amniotic + RUN apt -qq update -y -RUN apt -qq install -y pulseaudio vlc +RUN apt -qq install -y pulseaudio vlc alsa-utils + +COPY amniotic.client.conf /amniotic.client.conf FROM base AS development WORKDIR /usr/src diff --git a/amniotic.client.conf b/amniotic.client.conf new file mode 100644 index 0000000..7a0b444 --- /dev/null +++ b/amniotic.client.conf @@ -0,0 +1,6 @@ +default-server = unix:/amniotic.socket +# Prevent a server running in the container +autospawn = no +daemon-binary = /bin/true +# Prevent the use of shared memory +enable-shm = false \ No newline at end of file diff --git a/amniotic/mqtt/loop.py b/amniotic/mqtt/loop.py index 91c25cf..1285a28 100644 --- a/amniotic/mqtt/loop.py +++ b/amniotic/mqtt/loop.py @@ -1,4 +1,5 @@ from json import JSONDecodeError +from pathlib import Path from time import sleep import json @@ -73,7 +74,15 @@ def __init__(self, config: Config, device: Device, amniotic: Amniotic): if config.mqtt_username is not None and config.mqtt_password is not None: self.client.username_pw_set(username=config.mqtt_username, password=config.mqtt_password) - self.client.connect(host=config.mqtt_host, port=config.mqtt_port) + msg = f'Attempting to connect to MQTT "{config.mqtt_host}:{config.mqtt_port}"...' + logging.info(msg) + + try: + self.client.connect(host=config.mqtt_host, port=config.mqtt_port) + except Exception as exception: + msg = f'Error connecting. This usually means your MQTT host is not available.' + raise ConnectionError(msg) from exception + def on_message(self, client: mqtt.Client, amniotic: Amniotic, mqtt_message: mqtt.MQTTMessage): """ @@ -300,8 +309,4 @@ def start(): amniotic=amniotic, ) - loop.loop_start() - - -if __name__ == '__main__': - start() + loop.loop_start() \ No newline at end of file diff --git a/amniotic/mqtt/start.py b/amniotic/mqtt/start.py new file mode 100644 index 0000000..5c372a4 --- /dev/null +++ b/amniotic/mqtt/start.py @@ -0,0 +1,22 @@ +from pathlib import Path +import logging + +def start(): + + path_socket=Path('/amniotic.socket') + + if path_socket.exists(): + msg=f'Pulse socket found at "{path_socket}". Using host-shared audio.' + logging.info(msg) + import os + os.environ['PULSE_SERVER'] = "unix://amniotic.socket" + os.environ['PULSE_COOKIE'] = "/amniotic.cookie" + else: + msg = f'No Pulse socket found at "{path_socket}". Using dedicated audio.' + logging.warning(msg) + + from amniotic.mqtt import loop + loop.start() + +if __name__ == '__main__': + start() diff --git a/setup.py b/setup.py index 17b2d10..a20d70f 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,7 @@ }, entry_points={ 'console_scripts': [ - 'amniotic = amniotic.mqtt.loop:start', + 'amniotic = amniotic.mqtt.start:start', ], } )