aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/avpreviewoutput.py
blob: 2be4a52d8982137c19c1a6bb609ef6c8ec5a339b (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. if do_deinterlace:
  105. pipeline = '''
  106. deinterlace mode={imode} !
  107. videorate !
  108. '''
  109. else:
  110. pipeline = ''
  111. pipeline += '''
  112. videoscale !
  113. {target_caps} !
  114. jpegenc quality=90
  115. '''
  116. return pipeline.format(
  117. imode='interlaced' if do_deinterlace else 'disabled',
  118. target_caps=target_caps,
  119. )
  120. def on_accepted(self, conn, addr):
  121. self.log.debug('Adding fd %u to multifdsink', conn.fileno())
  122. fdsink = self.outputPipeline.get_by_name('fd')
  123. fdsink.emit('add', conn.fileno())
  124. def on_disconnect(multifdsink, fileno):
  125. if fileno == conn.fileno():
  126. self.log.debug('fd %u removed from multifdsink', fileno)
  127. self.close_connection(conn)
  128. fdsink.connect('client-fd-removed', on_disconnect)
  129. def on_eos(self, bus, message):
  130. self.log.debug('Received End-of-Stream-Signal on Output-Pipeline')
  131. def on_error(self, bus, message):
  132. self.log.debug('Received Error-Signal on Output-Pipeline')
  133. (error, debug) = message.parse_error()
  134. self.log.debug('Error-Details: #%u: %s', error.code, debug)