diff options
author | MaZderMind <github@mazdermind.de> | 2015-05-11 00:32:50 +0200 |
---|---|---|
committer | MaZderMind <github@mazdermind.de> | 2015-05-11 00:32:50 +0200 |
commit | 031632245e147e4532b252f91e31631570aa0fd3 (patch) | |
tree | 181833f353f7bbcf68a629bf323deba026d2d200 /voctocore/lib/audio/rawoutput.py | |
parent | 10058b623dee99ed54421ff204b51adcc9a379d0 (diff) |
a rudimentary audio pipeline
Diffstat (limited to 'voctocore/lib/audio/rawoutput.py')
-rw-r--r-- | voctocore/lib/audio/rawoutput.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/voctocore/lib/audio/rawoutput.py b/voctocore/lib/audio/rawoutput.py new file mode 100644 index 0000000..1ae22e3 --- /dev/null +++ b/voctocore/lib/audio/rawoutput.py @@ -0,0 +1,83 @@ +#!/usr/bin/python3 +import logging, socket +from gi.repository import GObject, Gst + +from lib.config import Config + +class AudioRawOutput(object): + log = logging.getLogger('AudioRawOutput') + + name = None + port = None + caps = None + + boundSocket = None + + receiverPipelines = [] + currentConnections = [] + + def __init__(self, channel, port, caps): + self.log = logging.getLogger('AudioRawOutput['+channel+']') + + self.channel = channel + self.port = port + self.caps = caps + + self.log.debug('Binding to Mirror-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) + + pipeline = """ + interaudiosrc channel={channel} ! + {caps} ! + gdppay ! + fdsink fd={fd} + """.format( + fd=conn.fileno(), + channel=self.channel, + caps=self.caps + ) + self.log.debug('Launching Pipeline:\n%s', pipeline) + receiverPipeline = Gst.parse_launch(pipeline) + + def on_eos(bus, message): + self.log.info('Received End-of-Stream-Signal on Source-Receiver-Pipeline') + self.disconnect(receiverPipeline, conn) + + def on_error(bus, message): + self.log.info('Received Error-Signal on Source-Receiver-Pipeline') + + (error, debug) = message.parse_error() + self.log.debug('Error-Message %s\n%s', error.message, debug) + + self.disconnect(receiverPipeline, conn) + + self.log.debug('Binding End-of-Stream-Signal on Pipeline') + receiverPipeline.bus.add_signal_watch() + receiverPipeline.bus.connect("message::eos", on_eos) + receiverPipeline.bus.connect("message::error", on_error) + + receiverPipeline.set_state(Gst.State.PLAYING) + + self.receiverPipelines.append(receiverPipeline) + self.currentConnections.append(conn) + + self.log.info('Now %u Receiver connected', len(self.currentConnections)) + + return True + + def disconnect(self, receiverPipeline, currentConnection): + receiverPipeline.set_state(Gst.State.NULL) + self.receiverPipelines.remove(receiverPipeline) + self.currentConnections.remove(currentConnection) + self.log.info('Disconnected Receiver, now %u Receiver connected', len(self.currentConnections)) |