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