summaryrefslogtreecommitdiff
path: root/voctocore/lib/audio/src.py
blob: 03c7bcc2a3f4fcb888a0b77e653199fa626963db (plain)
  1. #!/usr/bin/python3
  2. import logging, socket
  3. from gi.repository import GObject, Gst
  4. from lib.config import Config
  5. class AudioSrc(object):
  6. log = logging.getLogger('AudioSrc')
  7. name = None
  8. port = None
  9. caps = None
  10. receiverPipeline = None
  11. boundSocket = None
  12. currentConnection = None
  13. def __init__(self, name, port, caps):
  14. self.log = logging.getLogger('AudioSrc['+name+']')
  15. self.name = name
  16. self.port = port
  17. self.caps = caps
  18. self.log.debug('Binding to Source-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. if self.currentConnection is not None:
  30. self.log.warn("Another Source is already connected")
  31. return True
  32. pipeline = """
  33. fdsrc fd={fd} !
  34. gdpdepay !
  35. {caps} !
  36. queue !
  37. tee name=tee
  38. tee. ! queue ! interaudiosink channel=audio_{name}_mixer
  39. tee. ! queue ! interaudiosink channel=audio_{name}_mirror
  40. """.format(
  41. fd=conn.fileno(),
  42. name=self.name,
  43. caps=self.caps
  44. )
  45. self.log.debug('Launching Source-Receiver-Pipeline:\n%s', pipeline)
  46. self.receiverPipeline = Gst.parse_launch(pipeline)
  47. self.log.debug('Binding End-of-Stream-Signal on Source-Receiver-Pipeline')
  48. self.receiverPipeline.bus.add_signal_watch()
  49. self.receiverPipeline.bus.connect("message::eos", self.on_eos)
  50. self.receiverPipeline.bus.connect("message::error", self.on_error)
  51. self.receiverPipeline.set_state(Gst.State.PLAYING)
  52. self.currentConnection = conn
  53. return True
  54. def on_eos(self, bus, message):
  55. self.log.info('Received End-of-Stream-Signal on Source-Receiver-Pipeline')
  56. if self.currentConnection is not None:
  57. self.disconnect()
  58. def on_error(self, bus, message):
  59. self.log.info('Received Error-Signal on Source-Receiver-Pipeline')
  60. (code, debug) = message.parse_error()
  61. self.log.debug('Error-Details: #%u: %s', code, debug)
  62. if self.currentConnection is not None:
  63. self.disconnect()
  64. def disconnect(self):
  65. self.receiverPipeline.set_state(Gst.State.NULL)
  66. self.receiverPipeline = None
  67. self.currentConnection = None