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