aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/audio/src.py
diff options
context:
space:
mode:
authorMaZderMind <github@mazdermind.de>2015-05-11 00:32:50 +0200
committerMaZderMind <github@mazdermind.de>2015-05-11 00:32:50 +0200
commit031632245e147e4532b252f91e31631570aa0fd3 (patch)
tree181833f353f7bbcf68a629bf323deba026d2d200 /voctocore/lib/audio/src.py
parent10058b623dee99ed54421ff204b51adcc9a379d0 (diff)
a rudimentary audio pipeline
Diffstat (limited to 'voctocore/lib/audio/src.py')
-rw-r--r--voctocore/lib/audio/src.py102
1 files changed, 102 insertions, 0 deletions
diff --git a/voctocore/lib/audio/src.py b/voctocore/lib/audio/src.py
new file mode 100644
index 0000000..83805b9
--- /dev/null
+++ b/voctocore/lib/audio/src.py
@@ -0,0 +1,102 @@
+#!/usr/bin/python3
+import logging, socket
+from gi.repository import GObject, Gst
+
+from lib.config import Config
+
+class AudioSrc(object):
+ log = logging.getLogger('AudioSrc')
+
+ name = None
+ port = None
+ caps = None
+
+ distributionPipeline = None
+ receiverPipeline = None
+
+ boundSocket = None
+ currentConnection = None
+
+ def __init__(self, name, port, caps):
+ self.log = logging.getLogger('AudioSrc['+name+']')
+
+ self.name = name
+ self.port = port
+ self.caps = caps
+
+ pipeline = """
+ interaudiosrc channel=audio_{name}_in !
+ {caps} !
+ queue !
+ tee name=tee
+
+ tee. ! queue ! interaudiosink channel=audio_{name}_mirror
+ tee. ! queue ! interaudiosink channel=audio_{name}_preview
+ tee. ! queue ! interaudiosink channel=audio_{name}_mixer
+ """.format(
+ name=self.name,
+ caps=self.caps
+ )
+
+ self.log.debug('Launching Source-Distribution-Pipeline:\n%s', pipeline)
+ self.distributionPipeline = Gst.parse_launch(pipeline)
+ self.distributionPipeline.set_state(Gst.State.PLAYING)
+
+ self.log.debug('Binding to Source-Socket on [::]:%u', port)
+ self.boundSocket = socket.socket(socket.AF_INET6)
+ self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
+ self.boundSocket.bind(('::', port))
+ self.boundSocket.listen(1)
+
+ self.log.debug('Setting GObject io-watch on Socket')
+ GObject.io_add_watch(self.boundSocket, GObject.IO_IN, self.on_connect)
+
+ def on_connect(self, sock, *args):
+ conn, addr = sock.accept()
+ self.log.info("Incomming Connection from %s", addr)
+
+ if self.currentConnection is not None:
+ self.log.warn("Another Source is already connected")
+ return True
+
+ pipeline = """
+ fdsrc fd={fd} !
+ gdpdepay !
+ {caps} !
+ interaudiosink channel=audio_{name}_in
+ """.format(
+ fd=conn.fileno(),
+ name=self.name,
+ caps=self.caps
+ )
+ self.log.debug('Launching Source-Receiver-Pipeline:\n%s', pipeline)
+ self.receiverPipeline = Gst.parse_launch(pipeline)
+
+ self.log.debug('Binding End-of-Stream-Signal on Source-Receiver-Pipeline')
+ self.receiverPipeline.bus.add_signal_watch()
+ self.receiverPipeline.bus.connect("message::eos", self.on_eos)
+ self.receiverPipeline.bus.connect("message::error", self.on_error)
+
+ self.receiverPipeline.set_state(Gst.State.PLAYING)
+
+ self.currentConnection = conn
+ return True
+
+ def on_eos(self, bus, message):
+ self.log.info('Received End-of-Stream-Signal on Source-Receiver-Pipeline')
+ if self.currentConnection is not None:
+ self.disconnect()
+
+ def on_error(self, bus, message):
+ self.log.info('Received Error-Signal on Source-Receiver-Pipeline')
+ (code, debug) = message.parse_error()
+ self.log.debug('Error-Details: #%u: %s', code, debug)
+
+ if self.currentConnection is not None:
+ self.disconnect()
+
+ def disconnect(self):
+ self.receiverPipeline.set_state(Gst.State.NULL)
+ self.receiverPipeline = None
+ self.currentConnection = None