summaryrefslogtreecommitdiff
path: root/voctocore/lib/avpreviewoutput.py
blob: 2fa8c3a4a2df5c80808cd3d98461a63f1fadb031 (plain)
  1. #!/usr/bin/python3
  2. import logging, socket
  3. from gi.repository import GObject, Gst
  4. from lib.config import Config
  5. class AVPreviewOutput(object):
  6. log = logging.getLogger('AVPreviewOutput')
  7. name = None
  8. port = None
  9. caps = None
  10. boundSocket = None
  11. receiverPipeline = None
  12. currentConnections = []
  13. def __init__(self, channel, port):
  14. self.log = logging.getLogger('AVPreviewOutput['+channel+']')
  15. self.channel = channel
  16. self.port = port
  17. if Config.has_option('previews', 'videocaps'):
  18. vcaps_out = Config.get('previews', 'videocaps')
  19. else:
  20. vcaps_out = Config.get('mix', 'videocaps')
  21. pipeline = """
  22. interaudiosrc channel=audio_{channel} !
  23. {acaps} !
  24. queue !
  25. mux.
  26. intervideosrc channel=video_{channel} !
  27. {vcaps_in} !
  28. textoverlay halignment=left valignment=top ypad=75 text=AVPreviewOutput !
  29. timeoverlay halignment=left valignment=top ypad=75 xpad=400 !
  30. videorate !
  31. videoscale !
  32. {vcaps_out} !
  33. jpegenc !
  34. queue !
  35. mux.
  36. matroskamux
  37. name=mux
  38. streamable=true
  39. writing-app=Voctomix-AVPreviewOutput !
  40. multifdsink
  41. sync-method=next-keyframe
  42. name=fd
  43. """.format(
  44. channel=self.channel,
  45. acaps=Config.get('mix', 'audiocaps'),
  46. vcaps_in=Config.get('mix', 'videocaps'),
  47. vcaps_out=vcaps_out
  48. )
  49. self.log.debug('Launching Output-Pipeline:\n%s', pipeline)
  50. self.receiverPipeline = Gst.parse_launch(pipeline)
  51. self.receiverPipeline.set_state(Gst.State.PLAYING)
  52. self.log.debug('Binding to Output-Socket on [::]:%u', port)
  53. self.boundSocket = socket.socket(socket.AF_INET6)
  54. self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  55. self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
  56. self.boundSocket.bind(('::', port))
  57. self.boundSocket.listen(1)
  58. self.log.debug('Setting GObject io-watch on Socket')
  59. GObject.io_add_watch(self.boundSocket, GObject.IO_IN, self.on_connect)
  60. def on_connect(self, sock, *args):
  61. conn, addr = sock.accept()
  62. self.log.info("Incomming Connection from %s", addr)
  63. def on_disconnect(multifdsink, fileno):
  64. if fileno == conn.fileno():
  65. self.log.debug('fd %u removed from multifdsink', fileno)
  66. self.currentConnections.remove(conn)
  67. self.log.info('Disconnected Receiver %s', addr)
  68. self.log.info('Now %u Receiver connected', len(self.currentConnections))
  69. self.log.debug('Adding fd %u to multifdsink', conn.fileno())
  70. fdsink = self.receiverPipeline.get_by_name('fd')
  71. fdsink.emit('add', conn.fileno())
  72. fdsink.connect('client-fd-removed', on_disconnect)
  73. self.currentConnections.append(conn)
  74. self.log.info('Now %u Receiver connected', len(self.currentConnections))
  75. return True