summaryrefslogtreecommitdiff
path: root/voctocore/lib/avpreviewoutput.py
blob: 05f1cebb5f91f6afa9f796a4df5c559c6304694b (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. target_caps = Config.get('previews', 'videocaps')
  13. else:
  14. target_caps = Config.get('mix', 'videocaps')
  15. pipeline = """
  16. intervideosrc channel=video_{channel} !
  17. {vcaps} !
  18. {vpipeline} !
  19. queue !
  20. mux.
  21. interaudiosrc channel=audio_{channel} !
  22. {acaps} !
  23. queue !
  24. mux.
  25. matroskamux
  26. name=mux
  27. streamable=true
  28. writing-app=Voctomix-AVPreviewOutput !
  29. multifdsink
  30. blocksize=1048576
  31. buffers-max=500
  32. sync-method=next-keyframe
  33. name=fd
  34. """.format(
  35. channel=self.channel,
  36. acaps=Config.get('mix', 'audiocaps'),
  37. vcaps=Config.get('mix', 'videocaps'),
  38. vpipeline=self.construct_video_pipeline(target_caps)
  39. )
  40. self.log.debug('Creating Output-Pipeline:\n%s', pipeline)
  41. self.outputPipeline = Gst.parse_launch(pipeline)
  42. self.outputPipeline.use_clock(Clock)
  43. self.log.debug('Binding Error & End-of-Stream-Signal '
  44. 'on Output-Pipeline')
  45. self.outputPipeline.bus.add_signal_watch()
  46. self.outputPipeline.bus.connect("message::eos", self.on_eos)
  47. self.outputPipeline.bus.connect("message::error", self.on_error)
  48. self.log.debug('Launching Output-Pipeline')
  49. self.outputPipeline.set_state(Gst.State.PLAYING)
  50. def construct_video_pipeline(self, target_caps):
  51. vaapi_enabled = Config.has_option('previews', 'vaapi')
  52. if vaapi_enabled:
  53. return self.construct_vaapi_video_pipeline(target_caps)
  54. else:
  55. return self.construct_native_video_pipeline(target_caps)
  56. def construct_vaapi_video_pipeline(self, target_caps):
  57. if Gst.version() < (1, 8):
  58. vaapi_encoders = {
  59. 'h264': 'vaapiencode_h264',
  60. 'jpeg': 'vaapiencode_jpeg',
  61. 'mpeg2': 'vaapiencode_mpeg2',
  62. }
  63. else:
  64. vaapi_encoders = {
  65. 'h264': 'vaapih264enc',
  66. 'jpeg': 'vaapijpegenc',
  67. 'mpeg2': 'vaapimpeg2enc',
  68. }
  69. vaapi_encoder_options = {
  70. 'h264': 'rate-control=cqp init-qp=10 '
  71. 'max-bframes=0 keyframe-period=60',
  72. 'jpeg': 'vaapiencode_jpeg quality=90'
  73. 'keyframe-period=0',
  74. 'mpeg2': 'keyframe-period=60',
  75. }
  76. encoder = Config.get('previews', 'vaapi')
  77. do_deinterlace = Config.getboolean('previews', 'deinterlace')
  78. caps = Gst.Caps.from_string(target_caps)
  79. struct = caps.get_structure(0)
  80. _, width = struct.get_int('width')
  81. _, height = struct.get_int('height')
  82. _, framerate_numerator, framerate_denominator = struct.get_fraction('framerate')
  83. return '''
  84. capsfilter caps=video/x-raw,interlace-mode=progressive !
  85. vaapipostproc
  86. format=i420
  87. deinterlace-mode={imode}
  88. deinterlace-method=motion-adaptive
  89. width={width}
  90. height={height} !
  91. capssetter caps=video/x-raw,framerate={n}/{d} !
  92. {encoder} {options}
  93. '''.format(
  94. imode='interlaced' if do_deinterlace else 'disabled',
  95. width=width,
  96. height=height,
  97. encoder=vaapi_encoders[encoder],
  98. options=vaapi_encoder_options[encoder],
  99. n=framerate_numerator,
  100. d=framerate_denominator,
  101. )
  102. def construct_native_video_pipeline(self, target_caps):
  103. do_deinterlace = Config.getboolean('previews', 'deinterlace')
  104. return '''
  105. videoscale !
  106. {target_caps} !
  107. deinterlace mode={imode} !
  108. jpegenc quality=90
  109. '''.format(
  110. imode='interlaced' if do_deinterlace else 'disabled',
  111. target_caps=target_caps,
  112. )
  113. def on_accepted(self, conn, addr):
  114. self.log.debug('Adding fd %u to multifdsink', conn.fileno())
  115. fdsink = self.outputPipeline.get_by_name('fd')
  116. fdsink.emit('add', conn.fileno())
  117. def on_disconnect(multifdsink, fileno):
  118. if fileno == conn.fileno():
  119. self.log.debug('fd %u removed from multifdsink', fileno)
  120. self.close_connection(conn)
  121. fdsink.connect('client-fd-removed', on_disconnect)
  122. def on_eos(self, bus, message):
  123. self.log.debug('Received End-of-Stream-Signal on Output-Pipeline')
  124. def on_error(self, bus, message):
  125. self.log.debug('Received Error-Signal on Output-Pipeline')
  126. (error, debug) = message.parse_error()
  127. self.log.debug('Error-Details: #%u: %s', error.code, debug)