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