summaryrefslogtreecommitdiff
path: root/voctocore/lib/avpreviewoutput.py
blob: 749249b3f122f47f960bfd8e8a6a160e6857ed3c (plain)
  1. import logging
  2. from gi.repository import Gst
  3. from lib.config import Config
  4. from lib.tcpmulticonnection import TCPMultiConnection
  5. from lib.clock import Clock
  6. class AVPreviewOutput(TCPMultiConnection):
  7. def __init__(self, channel, port):
  8. self.log = logging.getLogger('AVPreviewOutput[{}]'.format(channel))
  9. super().__init__(port)
  10. self.channel = channel
  11. if Config.has_option('previews', 'videocaps'):
  12. vcaps_out = Config.get('previews', 'videocaps')
  13. else:
  14. vcaps_out = Config.get('mix', 'videocaps')
  15. deinterlace = ""
  16. if Config.getboolean('previews', 'deinterlace'):
  17. deinterlace = "deinterlace mode=interlaced !"
  18. pipeline = """
  19. intervideosrc channel=video_{channel} !
  20. {vcaps_in} !
  21. {deinterlace}
  22. videoscale !
  23. videorate !
  24. {vcaps_out} !
  25. jpegenc quality=90 !
  26. queue !
  27. mux.
  28. interaudiosrc channel=audio_{channel} !
  29. {acaps} !
  30. queue !
  31. mux.
  32. matroskamux
  33. name=mux
  34. streamable=true
  35. writing-app=Voctomix-AVPreviewOutput !
  36. multifdsink
  37. blocksize=1048576
  38. buffers-max=500
  39. sync-method=next-keyframe
  40. name=fd
  41. """.format(
  42. channel=self.channel,
  43. acaps=Config.get('mix', 'audiocaps'),
  44. vcaps_in=Config.get('mix', 'videocaps'),
  45. vcaps_out=vcaps_out,
  46. deinterlace=deinterlace
  47. )
  48. self.log.debug('Creating Output-Pipeline:\n%s', pipeline)
  49. self.outputPipeline = Gst.parse_launch(pipeline)
  50. self.outputPipeline.use_clock(Clock)
  51. self.log.debug('Binding Error & End-of-Stream-Signal '
  52. 'on Output-Pipeline')
  53. self.outputPipeline.bus.add_signal_watch()
  54. self.outputPipeline.bus.connect("message::eos", self.on_eos)
  55. self.outputPipeline.bus.connect("message::error", self.on_error)
  56. self.log.debug('Launching Output-Pipeline')
  57. self.outputPipeline.set_state(Gst.State.PLAYING)
  58. def on_accepted(self, conn, addr):
  59. self.log.debug('Adding fd %u to multifdsink', conn.fileno())
  60. fdsink = self.outputPipeline.get_by_name('fd')
  61. fdsink.emit('add', conn.fileno())
  62. def on_disconnect(multifdsink, fileno):
  63. if fileno == conn.fileno():
  64. self.log.debug('fd %u removed from multifdsink', fileno)
  65. self.close_connection(conn)
  66. fdsink.connect('client-fd-removed', on_disconnect)
  67. def on_eos(self, bus, message):
  68. self.log.debug('Received End-of-Stream-Signal on Output-Pipeline')
  69. def on_error(self, bus, message):
  70. self.log.debug('Received Error-Signal on Output-Pipeline')
  71. (error, debug) = message.parse_error()
  72. self.log.debug('Error-Details: #%u: %s', error.code, debug)