summaryrefslogtreecommitdiff
path: root/voctocore/lib/video/rawoutput.py
blob: bb9efc4be0f63c2a497c3cb1d05543ae79ae3786 (plain)
  1. #!/usr/bin/python3
  2. import logging, socket
  3. from gi.repository import GObject, Gst
  4. from lib.config import Config
  5. class VideoRawOutput(object):
  6. log = logging.getLogger('VideoRawOutput')
  7. name = None
  8. port = None
  9. caps = None
  10. boundSocket = None
  11. receiverPipelines = []
  12. currentConnections = []
  13. def __init__(self, channel, port, caps):
  14. self.log = logging.getLogger('VideoRawOutput['+channel+']')
  15. self.channel = channel
  16. self.port = port
  17. self.caps = caps
  18. self.log.debug('Binding to Mirror-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. pipeline = """
  30. intervideosrc channel={channel} !
  31. {caps} !
  32. textoverlay text={channel} halignment=left valignment=top ypad=225 !
  33. gdppay !
  34. fdsink fd={fd}
  35. """.format(
  36. fd=conn.fileno(),
  37. channel=self.channel,
  38. caps=self.caps
  39. )
  40. self.log.debug('Launching Pipeline:\n%s', pipeline)
  41. receiverPipeline = Gst.parse_launch(pipeline)
  42. def on_eos(bus, message):
  43. self.log.info('Received End-of-Stream-Signal on Source-Receiver-Pipeline')
  44. self.disconnect(receiverPipeline, conn)
  45. def on_error(bus, message):
  46. self.log.info('Received Error-Signal on Source-Receiver-Pipeline')
  47. (error, debug) = message.parse_error()
  48. self.log.debug('Error-Message %s\n%s', error.message, debug)
  49. self.disconnect(receiverPipeline, conn)
  50. self.log.debug('Binding End-of-Stream-Signal on Pipeline')
  51. receiverPipeline.bus.add_signal_watch()
  52. receiverPipeline.bus.connect("message::eos", on_eos)
  53. receiverPipeline.bus.connect("message::error", on_error)
  54. receiverPipeline.set_state(Gst.State.PLAYING)
  55. self.receiverPipelines.append(receiverPipeline)
  56. self.currentConnections.append(conn)
  57. self.log.info('Now %u Receiver connected', len(self.currentConnections))
  58. return True
  59. def disconnect(self, receiverPipeline, currentConnection):
  60. receiverPipeline.set_state(Gst.State.NULL)
  61. self.receiverPipelines.remove(receiverPipeline)
  62. self.currentConnections.remove(currentConnection)
  63. self.log.info('Disconnected Receiver, now %u Receiver connected', len(self.currentConnections))