summaryrefslogtreecommitdiff
path: root/voctocore/lib/videosrc.py
blob: 10b63fbdefc8b5a3047e9cfcc11a60894dec0b6c (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={name}_in !
  21. {caps} !
  22. timeoverlay halignment=left valignment=top !
  23. textoverlay text={name}_in halignment=left valignment=top ypad=75 !
  24. queue !
  25. tee name=tee
  26. tee. ! queue ! intervideosink channel={name}_mirror
  27. tee. ! queue ! intervideosink channel={name}_preview
  28. tee. ! queue ! intervideosink channel={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. intervideosink channel={name}_in
  55. """.format(
  56. fd=conn.fileno(),
  57. name=self.name,
  58. caps=self.caps
  59. )
  60. self.log.debug('Launching Source-Receiver-Pipeline:\n%s', pipeline)
  61. self.receiverPipeline = Gst.parse_launch(pipeline)
  62. self.log.debug('Binding End-of-Stream-Signal on Source-Receiver-Pipeline')
  63. self.receiverPipeline.bus.add_signal_watch()
  64. self.receiverPipeline.bus.connect("message::eos", self.on_disconnect)
  65. self.receiverPipeline.set_state(Gst.State.PLAYING)
  66. self.currentConnection = conn
  67. return True
  68. def on_disconnect(self, bus, message):
  69. self.log.info('Received End-of-Stream-Signal on Source-Receiver-Pipeline')
  70. self.receiverPipeline.set_state(Gst.State.NULL)
  71. self.receiverPipeline = None
  72. self.currentConnection = None