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