changeset 1469:94f14a0823c6

try using tcp to talk to host pulse Ignore-this: 330703f97be6f3e3490d0d1fb1c8f90f darcs-hash:40ee150e0987ebfb6c2d09fe6358e28b6273a04c
author drewp <drewp@bigasterisk.com>
date Sat, 21 Dec 2019 16:01:25 -0800
parents 32f12665a620
children 94abe0983594
files service/playSound/Dockerfile service/playSound/playSound.py service/playSound/requirements.txt service/playSound/run.sh service/playSound/serv.n3
diffstat 5 files changed, 46 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/service/playSound/Dockerfile	Fri Dec 20 17:17:56 2019 -0800
+++ b/service/playSound/Dockerfile	Sat Dec 21 16:01:25 2019 -0800
@@ -2,36 +2,36 @@
 
 WORKDIR /opt
 
-
-#RUN apt-get install -y libsdl-mixer-dev libsdl-mixer1.2-dev
-# wget https://www.pygame.org/ftp/pygame-1.9.6.tar.gz
-# 34  tar xvzf pygame-1.9.6.tar.gz
-# 35  cd pygame-1.9.6
-# python3 setup.py  install
-# python3 -c 'import pygame; print(pygame.__version__, pygame.__file__); pygame.mixer.init()'
-
-
 RUN touch need-new-update
 RUN apt-get update
+
 RUN apt-get install --yes libopenal1 libogg0 pulseaudio-utils python-pygame
+
+COPY pulse-client.conf /etc/pulse/client.conf
+COPY *.py *.wav ./
+
+ENV UNAME pulseuser
+RUN export UNAME=$UNAME UID=501 GID=501 && \
+    mkdir -p "/home/${UNAME}" && \
+    echo "${UNAME}:x:${UID}:${GID}:${UNAME} User,,,:/home/${UNAME}:/bin/bash" >> /etc/passwd && \
+    echo "${UNAME}:x:${UID}:" >> /etc/group && \
+    mkdir -p /etc/sudoers.d && \
+    echo "${UNAME} ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/${UNAME} && \
+    chmod 0440 /etc/sudoers.d/${UNAME} && \
+    chown ${UID}:${GID} -R /home/${UNAME} && \
+    gpasswd -a ${UNAME} audio
+# USER $UNAME
+
 COPY requirements.txt ./
 RUN pip3 install --index-url https://projects.bigasterisk.com/ --extra-index-url https://pypi.org/simple -r requirements.txt
 
-ENV SDL_AUDIODRIVER pulse
-COPY pulse-client.conf /etc/pulse/client.conf
-COPY *.py ./
+COPY run.sh ./
 
-# ENV UNAME pulseuser
-# RUN export UNAME=$UNAME UID=501 GID=501 && \
-#     mkdir -p "/home/${UNAME}" && \
-#     echo "${UNAME}:x:${UID}:${GID}:${UNAME} User,,,:/home/${UNAME}:/bin/bash" >> /etc/passwd && \
-#     echo "${UNAME}:x:${UID}:" >> /etc/group && \
-#     mkdir -p /etc/sudoers.d && \
-#     echo "${UNAME} ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/${UNAME} && \
-#     chmod 0440 /etc/sudoers.d/${UNAME} && \
-#     chown ${UID}:${GID} -R /home/${UNAME} && \
-#     gpasswd -a ${UNAME} audio
-# USER $UNAME
+ENV HOME /home/pulseuser
+RUN mkdir -p ${HOME}/.config/pulse
+RUN chown pulseuser ${HOME}
+
+ENV PULSE_SERVER 172.17.0.1
 
 EXPOSE 9049
 
--- a/service/playSound/playSound.py	Fri Dec 20 17:17:56 2019 -0800
+++ b/service/playSound/playSound.py	Sat Dec 21 16:01:25 2019 -0800
@@ -9,28 +9,29 @@
 from twisted.web.static import File
 from standardservice.logsetup import log, verboseLogging
 
-soundCount = itertools.count()
-
 class LOADING(object): pass
 
 class SoundEffects(object):
     def __init__(self):
-        self.buffers = {} # URIRef : pygame.mixer.Sound
+        self.buffers = {} # URIRef : path
         self.playingSources = []
         self.queued = []
         self.volume = 1 # level for the next sound that's played (or existing instances of the same sound)
 
     def _getSound(self, uri):
         def done(resp):
-            path = '/tmp/sound_%s' % next(soundCount)
-            with open(path, 'w') as out:
-                out.write(resp.body)
+            print('save')
+            body = bytes(resp.body)
+            path = '/tmp/sound_%s' % hash(uri)
+            with open(path, 'wb') as out:
+                out.write(body)
             log.info('write %s bytes to %s', len(resp.body), path)
-            self.buffers[uri] = pygame.mixer.Sound(path)
+            self.buffers[uri] = path
+            print('donesave')
 
-        return fetch(uri).addCallback(done).addErrback(log.error)
+        return fetch(uri.encode('utf8')).addCallback(done).addErrback(log.error)
 
-    def playEffect(self, uri):
+    def playEffect(self, uri: str):
         if uri not in self.buffers:
             self.buffers[uri] = LOADING
             self._getSound(uri).addCallback(lambda ret: self.playEffect(uri))
@@ -40,8 +41,9 @@
             # during that load are dropped, not queued.
             return
         snd = self.buffers[uri]
-        snd.set_volume(self.volume)
-        return self.playBuffer(snd)
+        print('subp')
+        subprocess.check_call(['paplay', snd])
+        return
 
     def done(self, src):
         try:
@@ -56,7 +58,6 @@
             q.cancel()
         # doesn't cover the callLater ones
 
-
 class Index(cyclone.web.RequestHandler):
     def get(self):
         self.render('index.html')
@@ -86,10 +87,7 @@
     ''')
     verboseLogging(arg['-v'])
 
-    import pygame
-    print('mixer init pulse')
-    import pygame.mixer
-    pygame.mixer.init()
+    os.environ['PULSE_SERVER'] = '172.17.0.1'
     sfx = SoundEffects()
 
     reactor.listenTCP(9049, cyclone.web.Application(handlers=[
--- a/service/playSound/requirements.txt	Fri Dec 20 17:17:56 2019 -0800
+++ b/service/playSound/requirements.txt	Sat Dec 21 16:01:25 2019 -0800
@@ -1,10 +1,6 @@
 cyclone
 mock==1.0.1
 Twisted
-
-# upgrading to 1.9.5 makes it stop working with pulseaudio
-# (maybe this unversioned one picks the system package which
-# has a better SDL build)
-pygame
+requests==2.22.0
 
 standardservice==0.6.0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/service/playSound/run.sh	Sat Dec 21 16:01:25 2019 -0800
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+
+paplay aud.wav
--- a/service/playSound/serv.n3	Fri Dec 20 17:17:56 2019 -0800
+++ b/service/playSound/serv.n3	Sat Dec 21 16:01:25 2019 -0800
@@ -14,11 +14,13 @@
       "--cap-add" "SYS_PTRACE"
       #"--mount type=bind,source=/etc/pulse,target=/etc/pulse"
       "--mount" "type=tmpfs,destination=/tmp,tmpfs-size=52428800"
-      "--mount" "type=bind,source=/run/user/501/pulse,target=/run/user/501/pulse"
+      "--mount" "type=bind,source=/home/drewp/.config/pulse/cookie,target=/home/pulseuser/.config/pulse/cookie"
       );
       :localRunCmdline (
+#      "./run.sh"
 #      "strace" "-ftts" "999"
-      "python3" "playSound.py" "-v");
+      "python3" "playSound.py" "-v"
+);
       :dockerFile "Dockerfile"
 .