summaryrefslogtreecommitdiff
path: root/voctocore/lib/video/src.py
blob: cd36d8c64e0364d2a1c45b80a81d71060ccbe854 (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. receiverPipeline = None
  11. boundSocket = None
  12. currentConnection = None
  13. def __init__(self, name, port, caps):
  14. self.log = logging.getLogger('VideoSrc['+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. textoverlay text=video_{name}_fd halignment=left valignment=top ypad=125 !
  37. queue !
  38. tee name=tee
  39. tee. ! queue ! intervideosink channel=video_{name}_mixer
  40. tee. ! queue ! intervideosink channel=video_{name}_mirror
  41. """.format(
  42. fd=conn.fileno(),
  43. name=self.name,
  44. caps=self.caps
  45. )
  46. self.log.debug('Launching Source-Receiver-Pipeline:\n%s', pipeline)
  47. self.receiverPipeline = Gst.parse_launch(pipeline)
  48. self.log.debug('Binding End-of-Stream-Signal on Source-Receiver-Pipeline')
  49. self.receiverPipeline.bus.add_signal_watch()
  50. self.receiverPipeline.bus.connect("message::eos", self.on_eos)
  51. self.receiverPipeline.bus.connect("message::error", self.on_error)
  52. self.receiverPipeline.set_state(Gst.State.PLAYING)
  53. self.currentConnection = conn
  54. return True
  55. def on_eos(self, bus, message):
  56. self.log.info('Received End-of-Stream-Signal on Source-Receiver-Pipeline')
  57. if self.currentConnection is not None:
  58. self.disconnect()
  59. def on_error(self, bus, message):
  60. self.log.info('Received Error-Signal on Source-Receiver-Pipeline')
  61. (code, debug) = message.parse_error()
  62. self.log.debug('Error-Details: #%u: %s', code, debug)
  63. if self.currentConnection is not None:
  64. self.disconnect()
  65. def disconnect(self):
  66. self.receiverPipeline.set_state(Gst.State.NULL)
  67. self.receiverPipeline = None
  68. self.currentConnection = None