summaryrefslogtreecommitdiff
path: root/voctocore/lib/audio/src.py
blob: 83805b96b98986446454b2a5fecba3ad0fd987b6 (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. distributionPipeline = None
  11. receiverPipeline = None
  12. boundSocket = None
  13. currentConnection = None
  14. def __init__(self, name, port, caps):
  15. self.log = logging.getLogger('AudioSrc['+name+']')
  16. self.name = name
  17. self.port = port
  18. self.caps = caps
  19. pipeline = """
  20. interaudiosrc channel=audio_{name}_in !
  21. {caps} !
  22. queue !
  23. tee name=tee
  24. tee. ! queue ! interaudiosink channel=audio_{name}_mirror
  25. tee. ! queue ! interaudiosink channel=audio_{name}_preview
  26. tee. ! queue ! interaudiosink channel=audio_{name}_mixer
  27. """.format(
  28. name=self.name,
  29. caps=self.caps
  30. )
  31. self.log.debug('Launching Source-Distribution-Pipeline:\n%s', pipeline)
  32. self.distributionPipeline = Gst.parse_launch(pipeline)
  33. self.distributionPipeline.set_state(Gst.State.PLAYING)
  34. self.log.debug('Binding to Source-Socket on [::]:%u', port)
  35. self.boundSocket = socket.socket(socket.AF_INET6)
  36. self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  37. self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
  38. self.boundSocket.bind(('::', port))
  39. self.boundSocket.listen(1)
  40. self.log.debug('Setting GObject io-watch on Socket')
  41. GObject.io_add_watch(self.boundSocket, GObject.IO_IN, self.on_connect)
  42. def on_connect(self, sock, *args):
  43. conn, addr = sock.accept()
  44. self.log.info("Incomming Connection from %s", addr)
  45. if self.currentConnection is not None:
  46. self.log.warn("Another Source is already connected")
  47. return True
  48. pipeline = """
  49. fdsrc fd={fd} !
  50. gdpdepay !
  51. {caps} !
  52. interaudiosink channel=audio_{name}_in
  53. """.format(
  54. fd=conn.fileno(),
  55. name=self.name,
  56. caps=self.caps
  57. )
  58. self.log.debug('Launching Source-Receiver-Pipeline:\n%s', pipeline)
  59. self.receiverPipeline = Gst.parse_launch(pipeline)
  60. self.log.debug('Binding End-of-Stream-Signal on Source-Receiver-Pipeline')
  61. self.receiverPipeline.bus.add_signal_watch()
  62. self.receiverPipeline.bus.connect("message::eos", self.on_eos)
  63. self.receiverPipeline.bus.connect("message::error", self.on_error)
  64. self.receiverPipeline.set_state(Gst.State.PLAYING)
  65. self.currentConnection = conn
  66. return True
  67. def on_eos(self, bus, message):
  68. self.log.info('Received End-of-Stream-Signal on Source-Receiver-Pipeline')
  69. if self.currentConnection is not None:
  70. self.disconnect()
  71. def on_error(self, bus, message):
  72. self.log.info('Received Error-Signal on Source-Receiver-Pipeline')
  73. (code, debug) = message.parse_error()
  74. self.log.debug('Error-Details: #%u: %s', code, debug)
  75. if self.currentConnection is not None:
  76. self.disconnect()
  77. def disconnect(self):
  78. self.receiverPipeline.set_state(Gst.State.NULL)
  79. self.receiverPipeline = None
  80. self.currentConnection = None