summaryrefslogtreecommitdiff
path: root/voctocore/lib/audio/rawoutput.py
blob: e268827c2bb7368de020432c8645a8523965ddde (plain)
  1. #!/usr/bin/python3
  2. import logging, socket
  3. from gi.repository import GObject, Gst
  4. from lib.config import Config
  5. class AudioRawOutput(object):
  6. log = logging.getLogger('AudioRawOutput')
  7. name = None
  8. port = None
  9. caps = None
  10. boundSocket = None
  11. receiverPipeline = None
  12. currentConnections = []
  13. def __init__(self, channel, port, caps):
  14. self.log = logging.getLogger('AudioRawOutput['+channel+']')
  15. self.channel = channel
  16. self.port = port
  17. self.caps = caps
  18. pipeline = """
  19. interaudiosrc channel={channel} !
  20. {caps} !
  21. gdppay !
  22. multifdsink resend-streamheader=false name=fd
  23. """.format(
  24. channel=self.channel,
  25. caps=self.caps
  26. )
  27. self.log.debug('Launching Pipeline:\n%s', pipeline)
  28. self.receiverPipeline = Gst.parse_launch(pipeline)
  29. self.receiverPipeline.bus.add_signal_watch()
  30. self.receiverPipeline.set_state(Gst.State.PLAYING)
  31. self.log.debug('Binding to Mirror-Socket on [::]:%u', port)
  32. self.boundSocket = socket.socket(socket.AF_INET6)
  33. self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  34. self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
  35. self.boundSocket.bind(('::', port))
  36. self.boundSocket.listen(1)
  37. self.log.debug('Setting GObject io-watch on Socket')
  38. GObject.io_add_watch(self.boundSocket, GObject.IO_IN, self.on_connect)
  39. def on_connect(self, sock, *args):
  40. conn, addr = sock.accept()
  41. self.log.info("Incomming Connection from %s", addr)
  42. self.log.info('Adding fd %u to multifdsink', conn.fileno())
  43. self.receiverPipeline.get_by_name('fd').emit('add', conn.fileno())
  44. self.currentConnections.append(conn)
  45. self.log.info('Now %u Receiver connected', len(self.currentConnections))
  46. return True
  47. def disconnect(self, receiverPipeline, currentConnection):
  48. self.currentConnections.remove(currentConnection)
  49. self.log.info('Disconnected Receiver, now %u Receiver connected', len(self.currentConnections))