summaryrefslogtreecommitdiff
path: root/voctocore/lib/avpreviewoutput.py
blob: b2e8b4a21beadc662072b2b63bd0839d9c11bb10 (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. venc = 'jpegenc quality=90'
  19. try:
  20. encoder = Config.get('previews', 'vaapi')
  21. encoders = {
  22. 'h264': 'vaapih264enc',
  23. 'jpeg': 'vaapijpegenc',
  24. 'mpeg2': 'vaapimpeg2enc',
  25. }
  26. venc = encoders[encoder]
  27. except Exception as e:
  28. self.log.error(e)
  29. pipeline = """
  30. intervideosrc channel=video_{channel} !
  31. {vcaps_in} !
  32. {deinterlace}
  33. videoscale !
  34. videorate !
  35. {vcaps_out} !
  36. {venc} !
  37. queue !
  38. mux.
  39. interaudiosrc channel=audio_{channel} !
  40. {acaps} !
  41. queue !
  42. mux.
  43. matroskamux
  44. name=mux
  45. streamable=true
  46. writing-app=Voctomix-AVPreviewOutput !
  47. multifdsink
  48. blocksize=1048576
  49. buffers-max=500
  50. sync-method=next-keyframe
  51. name=fd
  52. """.format(
  53. channel=self.channel,
  54. acaps=Config.get('mix', 'audiocaps'),
  55. vcaps_in=Config.get('mix', 'videocaps'),
  56. vcaps_out=vcaps_out,
  57. deinterlace=deinterlace,
  58. venc=venc
  59. )
  60. self.log.debug('Creating Output-Pipeline:\n%s', pipeline)
  61. self.outputPipeline = Gst.parse_launch(pipeline)
  62. self.outputPipeline.use_clock(Clock)
  63. self.log.debug('Binding Error & End-of-Stream-Signal '
  64. 'on Output-Pipeline')
  65. self.outputPipeline.bus.add_signal_watch()
  66. self.outputPipeline.bus.connect("message::eos", self.on_eos)
  67. self.outputPipeline.bus.connect("message::error", self.on_error)
  68. self.log.debug('Launching Output-Pipeline')
  69. self.outputPipeline.set_state(Gst.State.PLAYING)
  70. def on_accepted(self, conn, addr):
  71. self.log.debug('Adding fd %u to multifdsink', conn.fileno())
  72. fdsink = self.outputPipeline.get_by_name('fd')
  73. fdsink.emit('add', conn.fileno())
  74. def on_disconnect(multifdsink, fileno):
  75. if fileno == conn.fileno():
  76. self.log.debug('fd %u removed from multifdsink', fileno)
  77. self.close_connection(conn)
  78. fdsink.connect('client-fd-removed', on_disconnect)
  79. def on_eos(self, bus, message):
  80. self.log.debug('Received End-of-Stream-Signal on Output-Pipeline')
  81. def on_error(self, bus, message):
  82. self.log.debug('Received Error-Signal on Output-Pipeline')
  83. (error, debug) = message.parse_error()
  84. self.log.debug('Error-Details: #%u: %s', error.code, debug)