aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/avpreviewoutput.py
blob: fc46dcce4e13abce659bc30c555bd99c8fc49743 (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,
  83. framerate_denominator) = struct.get_fraction('framerate')
  84. return '''
  85. capsfilter caps=video/x-raw,interlace-mode=progressive !
  86. vaapipostproc
  87. format=i420
  88. deinterlace-mode={imode}
  89. deinterlace-method=motion-adaptive
  90. width={width}
  91. height={height} !
  92. capssetter caps=video/x-raw,framerate={n}/{d} !
  93. {encoder} {options}
  94. '''.format(
  95. imode='interlaced' if do_deinterlace else 'disabled',
  96. width=width,
  97. height=height,
  98. encoder=vaapi_encoders[encoder],
  99. options=vaapi_encoder_options[encoder],
  100. n=framerate_numerator,
  101. d=framerate_denominator,
  102. )
  103. def construct_native_video_pipeline(self, target_caps):
  104. do_deinterlace = Config.getboolean('previews', 'deinterlace')
  105. if do_deinterlace:
  106. pipeline = '''
  107. deinterlace mode={imode} !
  108. videorate !
  109. '''
  110. else:
  111. pipeline = ''
  112. pipeline += '''
  113. videoscale !
  114. {target_caps} !
  115. jpegenc quality=90
  116. '''
  117. return pipeline.format(
  118. imode='interlaced' if do_deinterlace else 'disabled',
  119. target_caps=target_caps,
  120. )
  121. def on_accepted(self, conn, addr):
  122. self.log.debug('Adding fd %u to multifdsink', conn.fileno())
  123. fdsink = self.outputPipeline.get_by_name('fd')
  124. fdsink.emit('add', conn.fileno())
  125. def on_disconnect(multifdsink, fileno):
  126. if fileno == conn.fileno():
  127. self.log.debug('fd %u removed from multifdsink', fileno)
  128. self.close_connection(conn)
  129. fdsink.connect('client-fd-removed', on_disconnect)
  130. def on_eos(self, bus, message):
  131. self.log.debug('Received End-of-Stream-Signal on Output-Pipeline')
  132. def on_error(self, bus, message):
  133. self.log.debug('Received Error-Signal on Output-Pipeline')
  134. (error, debug) = message.parse_error()
  135. self.log.debug('Error-Details: #%u: %s', error.code, debug)