aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/avsource.py
blob: a80994877dcdff0ea66c1e2f96126dd9c85e73f1 (plain)
  1. #!/usr/bin/python3
  2. import logging, socket
  3. from gi.repository import GObject, Gst
  4. from lib.config import Config
  5. class AVSource(object):
  6. log = logging.getLogger('AVSource')
  7. name = None
  8. port = None
  9. caps = None
  10. receiverPipeline = None
  11. boundSocket = None
  12. currentConnection = None
  13. def __init__(self, name, port):
  14. self.log = logging.getLogger('AVSource['+name+']')
  15. self.name = name
  16. self.port = port
  17. self.log.debug('Binding to Source-Socket on [::]:%u', port)
  18. self.boundSocket = socket.socket(socket.AF_INET6)
  19. self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  20. self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
  21. self.boundSocket.bind(('::', port))
  22. self.boundSocket.listen(1)
  23. self.log.debug('Setting GObject io-watch on Socket')
  24. GObject.io_add_watch(self.boundSocket, GObject.IO_IN, self.on_connect)
  25. def on_connect(self, sock, *args):
  26. conn, addr = sock.accept()
  27. self.log.info("Incomming Connection from %s", addr)
  28. if self.currentConnection is not None:
  29. self.log.warn("Another Source is already connected")
  30. return True
  31. pipeline = """
  32. fdsrc fd={fd} !
  33. matroskademux name=demux
  34. demux. !
  35. {acaps} !
  36. queue !
  37. tee name=atee
  38. atee. ! queue ! interaudiosink channel=audio_{name}_mixer
  39. atee. ! queue ! interaudiosink channel=audio_{name}_mirror
  40. """.format(
  41. fd=conn.fileno(),
  42. name=self.name,
  43. acaps=Config.get('mix', 'audiocaps')
  44. )
  45. if Config.getboolean('previews', 'enabled'):
  46. pipeline += """
  47. atee. ! queue ! interaudiosink channel=audio_{name}_preview
  48. """.format(
  49. name=self.name
  50. )
  51. pipeline += """
  52. demux. !
  53. {vcaps} !
  54. textoverlay halignment=left valignment=top ypad=25 text=AVSource !
  55. timeoverlay halignment=left valignment=top ypad=25 xpad=400 !
  56. queue !
  57. tee name=vtee
  58. vtee. ! queue ! intervideosink channel=video_{name}_mixer
  59. vtee. ! queue ! intervideosink channel=video_{name}_mirror
  60. """.format(
  61. fd=conn.fileno(),
  62. name=self.name,
  63. vcaps=Config.get('mix', 'videocaps')
  64. )
  65. if Config.getboolean('previews', 'enabled'):
  66. pipeline += """
  67. vtee. ! queue ! intervideosink channel=video_{name}_preview
  68. """.format(
  69. name=self.name
  70. )
  71. self.log.debug('Launching Source-Pipeline:\n%s', pipeline)
  72. self.receiverPipeline = Gst.parse_launch(pipeline)
  73. self.log.debug('Binding End-of-Stream-Signal on Source-Pipeline')
  74. self.receiverPipeline.bus.add_signal_watch()
  75. self.receiverPipeline.bus.connect("message::eos", self.on_eos)
  76. self.receiverPipeline.bus.connect("message::error", self.on_error)
  77. self.receiverPipeline.set_state(Gst.State.PLAYING)
  78. self.currentConnection = conn
  79. return True
  80. def on_eos(self, bus, message):
  81. self.log.debug('Received End-of-Stream-Signal on Source-Pipeline')
  82. if self.currentConnection is not None:
  83. self.disconnect()
  84. def on_error(self, bus, message):
  85. self.log.debug('Received Error-Signal on Source-Pipeline')
  86. (error, debug) = message.parse_error()
  87. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  88. if self.currentConnection is not None:
  89. self.disconnect()
  90. def disconnect(self):
  91. self.log.info('Connection closed')
  92. self.receiverPipeline.set_state(Gst.State.NULL)
  93. self.receiverPipeline = None
  94. self.currentConnection = None