summaryrefslogtreecommitdiff
path: root/voctocore/lib/videomix.py
blob: d9969674cec7ebda49cee9be8f5ccadf072d9688 (plain)
  1. #!/usr/bin/python3
  2. import logging
  3. from gi.repository import Gst
  4. from enum import Enum
  5. from lib.config import Config
  6. class CompositeModes(Enum):
  7. fullscreen = 0
  8. side_by_side_equal = 1
  9. class VideoMix(object):
  10. log = logging.getLogger('VideoMix')
  11. def __init__(self):
  12. self.caps = Config.get('mix', 'videocaps')
  13. self.names = Config.getlist('mix', 'sources')
  14. self.log.info('Configuring Mixer for %u Sources', len(self.names))
  15. pipeline = """
  16. videomixer name=mix !
  17. {caps} !
  18. queue !
  19. tee name=tee
  20. tee. ! queue ! intervideosink channel=video_mix_out
  21. """.format(
  22. caps=self.caps
  23. )
  24. if Config.getboolean('previews', 'enabled'):
  25. pipeline += """
  26. tee. ! queue ! intervideosink channel=video_mix_preview
  27. """
  28. for idx, name in enumerate(self.names):
  29. pipeline += """
  30. intervideosrc channel=video_{name}_mixer !
  31. {caps} !
  32. videoscale !
  33. capsfilter name=caps_{idx} !
  34. mix.
  35. """.format(
  36. name=name,
  37. caps=self.caps,
  38. idx=idx
  39. )
  40. self.log.debug('Creating Mixing-Pipeline:\n%s', pipeline)
  41. self.mixingPipeline = Gst.parse_launch(pipeline)
  42. self.log.debug('Binding Error & End-of-Stream-Signal on Mixing-Pipeline')
  43. self.mixingPipeline.bus.add_signal_watch()
  44. self.mixingPipeline.bus.connect("message::eos", self.on_eos)
  45. self.mixingPipeline.bus.connect("message::error", self.on_error)
  46. self.log.debug('Initializing Mixer-State')
  47. self.compositeMode = CompositeModes.fullscreen
  48. self.sourceA = 0
  49. self.sourceB = 1
  50. self.updateMixerState()
  51. self.log.debug('Launching Mixing-Pipeline')
  52. self.mixingPipeline.set_state(Gst.State.PLAYING)
  53. def getInputVideoSize(self):
  54. caps = Gst.Caps.from_string(self.caps)
  55. struct = caps.get_structure(0)
  56. _, width = struct.get_int('width')
  57. _, height = struct.get_int('height')
  58. return width, height
  59. def updateMixerState(self):
  60. if self.compositeMode == CompositeModes.fullscreen:
  61. self.updateMixerStateFullscreen()
  62. if self.compositeMode == CompositeModes.side_by_side_equal:
  63. self.updateMixerStateSideBySideEqual()
  64. def updateMixerStateFullscreen(self):
  65. self.log.info('Updating Mixer-State for Fullscreen-Composition')
  66. noScaleCaps = Gst.Caps.from_string('video/x-raw')
  67. for idx, name in enumerate(self.names):
  68. alpha = int(idx == self.sourceA)
  69. self.log.debug('Setting Mixerpad %u to x/y=0 and alpha=%0.2f', idx, alpha)
  70. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  71. mixerpad.set_property('alpha', alpha )
  72. mixerpad.set_property('xpos', 0)
  73. mixerpad.set_property('ypos', 0)
  74. self.log.debug('Resetting Scaler %u to non-scaling', idx)
  75. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  76. capsfilter.set_property('caps', noScaleCaps)
  77. def updateMixerStateSideBySideEqual(self):
  78. self.log.info('Updating Mixer-State for Side-by-side-Equal-Composition')
  79. width, height = self.getInputVideoSize()
  80. self.log.debug('Video-Size parsed as %ux%u', width, height)
  81. try:
  82. gutter = Config.getint('side-by-side-equal', 'gutter')
  83. self.log.debug('Gutter configured to %u', gutter)
  84. except:
  85. gutter = int(width / 100)
  86. self.log.debug('Gutter calculated to %u', gutter)
  87. targetWidth = int((width - gutter) / 2)
  88. targetHeight = int(targetWidth / width * height)
  89. y = (height - targetHeight) / 2
  90. xa = 0
  91. xb = width - targetWidth
  92. scaleCaps = Gst.Caps.from_string('video/x-raw,width=%u,height=%u' % (targetWidth, targetHeight))
  93. noScaleCaps = Gst.Caps.from_string('video/x-raw')
  94. for idx, name in enumerate(self.names):
  95. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  96. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  97. if idx == self.sourceA:
  98. mixerpad.set_property('alpha', 1)
  99. mixerpad.set_property('xpos', xa)
  100. mixerpad.set_property('ypos', y)
  101. capsfilter.set_property('caps', scaleCaps)
  102. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, xa, y, 1)
  103. self.log.debug('Setting Scaler %u to %u/%u', idx, targetWidth, targetHeight)
  104. elif idx == self.sourceB:
  105. mixerpad.set_property('alpha', 1)
  106. mixerpad.set_property('xpos', xb)
  107. mixerpad.set_property('ypos', y)
  108. capsfilter.set_property('caps', scaleCaps)
  109. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, xb, y, 1)
  110. self.log.debug('Setting Scaler %u to %u/%u', idx, targetWidth, targetHeight)
  111. else:
  112. mixerpad.set_property('alpha', 0)
  113. mixerpad.set_property('xpos', 0)
  114. mixerpad.set_property('ypos', 0)
  115. capsfilter.set_property('caps', noScaleCaps)
  116. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, 0, 0, 0)
  117. self.log.debug('Resetting Scaler %u to non-scaling', idx)
  118. def setVideoSourceA(self, source):
  119. # swap if required
  120. if self.sourceB == source:
  121. self.sourceB = self.sourceA
  122. self.sourceA = source
  123. self.updateMixerState()
  124. def setVideoSourceB(self, source):
  125. # swap if required
  126. if self.sourceA == source:
  127. self.sourceA = self.sourceB
  128. self.sourceB = source
  129. self.updateMixerState()
  130. def setCompositeMode(self, mode):
  131. self.compositeMode = mode
  132. self.updateMixerState()
  133. def on_eos(self, bus, message):
  134. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  135. def on_error(self, bus, message):
  136. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  137. (error, debug) = message.parse_error()
  138. self.log.debug('Error-Details: #%u: %s', error.code, debug)