Skip to content

Commit

Permalink
Support sharing Docker audio with host
Browse files Browse the repository at this point in the history
  • Loading branch information
ejohb committed Nov 4, 2023
1 parent b66d2a3 commit 4eca569
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions amniotic.client.conf
Original file line number Diff line number Diff line change
@@ -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
17 changes: 11 additions & 6 deletions amniotic/mqtt/loop.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from json import JSONDecodeError
from pathlib import Path
from time import sleep

import json
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -300,8 +309,4 @@ def start():
amniotic=amniotic,
)

loop.loop_start()


if __name__ == '__main__':
start()
loop.loop_start()
22 changes: 22 additions & 0 deletions amniotic/mqtt/start.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
entry_points={
'console_scripts': [
'amniotic = amniotic.mqtt.loop:start',
'amniotic = amniotic.mqtt.start:start',
],
}
)

0 comments on commit 4eca569

Please sign in to comment.