summaryrefslogtreecommitdiff
path: root/voctocore/lib/audio/rawoutput.py
blob: 1ae22e3e8955b79fe2dcd15a2c510832ee2847c6 (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. receiverPipelines = []
  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. self.log.debug('Binding to Mirror-Socket on [::]:%u', port)
  19. self.boundSocket = socket.socket(socket.AF_INET6)
  20. self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  21. self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
  22. self.boundSocket.bind(('::', port))
  23. self.boundSocket.listen(1)
  24. self.log.debug('Setting GObject io-watch on Socket')
  25. GObject.io_add_watch(self.boundSocket, GObject.IO_IN, self.on_connect)
  26. def on_connect(self, sock, *args):
  27. conn, addr = sock.accept()
  28. self.log.info("Incomming Connection from %s", addr)
  29. pipeline = """
  30. interaudiosrc channel={channel} !
  31. {caps} !
  32. gdppay !
  33. fdsink fd={fd}
  34. """.format(
  35. fd=conn.fileno(),
  36. channel=self.channel,
  37. caps=self.caps
  38. )
  39. self.log.debug('Launching Pipeline:\n%s', pipeline)
  40. receiverPipeline = Gst.parse_launch(pipeline)
  41. def on_eos(bus, message):
  42. self.log.info('Received End-of-Stream-Signal on Source-Receiver-Pipeline')
  43. self.disconnect(receiverPipeline, conn)
  44. def on_error(bus, message):
  45. self.log.info('Received Error-Signal on Source-Receiver-Pipeline')
  46. (error, debug) = message.parse_error()
  47. self.log.debug('Error-Message %s\n%s', error.message, debug)
  48. self.disconnect(receiverPipeline, conn)
  49. self.log.debug('Binding End-of-Stream-Signal on Pipeline')
  50. receiverPipeline.bus.add_signal_watch()
  51. receiverPipeline.bus.connect("message::eos", on_eos)
  52. receiverPipeline.bus.connect("message::error", on_error)
  53. receiverPipeline.set_state(Gst.State.PLAYING)
  54. self.receiverPipelines.append(receiverPipeline)
  55. self.currentConnections.append(conn)
  56. self.log.info('Now %u Receiver connected', len(self.currentConnections))
  57. return True
  58. def disconnect(self, receiverPipeline, currentConnection):
  59. receiverPipeline.set_state(Gst.State.NULL)
  60. self.receiverPipelines.remove(receiverPipeline)
  61. self.currentConnections.remove(currentConnection)
  62. self.log.info('Disconnected Receiver, now %u Receiver connected', len(self.currentConnections))