summaryrefslogtreecommitdiff
path: root/voctocore/lib/videomix.py
blob: ffdbef22c17e5ba888c3c2f810e9e1ad4f77d7bb (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 updateMixerState(self):
  54. if self.compositeMode == CompositeModes.fullscreen:
  55. self.updateMixerStateFullscreen()
  56. if self.compositeMode == CompositeModes.side_by_side_equal:
  57. self.updateMixerStateSideBySideEqual()
  58. def updateMixerStateFullscreen(self):
  59. self.log.info('Updating Mixer-State for Fullscreen-Composition')
  60. noScaleCaps = Gst.Caps.from_string('video/x-raw')
  61. for idx, name in enumerate(self.names):
  62. alpha = int(idx == self.sourceA)
  63. self.log.debug('Setting Mixerpad %u to x/y=0 and alpha=%0.2f', idx, alpha)
  64. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  65. mixerpad.set_property('alpha', alpha )
  66. mixerpad.set_property('xpos', 0)
  67. mixerpad.set_property('ypos', 0)
  68. self.log.debug('Resetting Scaler %u to non-scaling', idx)
  69. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  70. capsfilter.set_property('caps', noScaleCaps)
  71. def getInputVideoSize(self):
  72. caps = Gst.Caps.from_string(self.caps)
  73. struct = caps.get_structure(0)
  74. _, width = struct.get_int('width')
  75. _, height = struct.get_int('height')
  76. return width, height
  77. def updateMixerStateSideBySideEqual(self):
  78. self.log.info('Updating Mixer-State for Side-by-side-Equal-Composition')
  79. width, height = self.getInputVideoSize()
  80. gutter = int(width / 100)
  81. self.log.debug('Video-Size parsed as %u/%u, Gutter calculated to %upx', width, height, gutter)
  82. targetWidth = int((width - gutter) / 2)
  83. targetHeight = int(targetWidth / width * height)
  84. ycenter = (height - targetHeight) / 2
  85. xa = 0
  86. xb = width - targetWidth
  87. scaleCaps = Gst.Caps.from_string('video/x-raw,width=%u,height=%u' % (targetWidth, targetHeight))
  88. noScaleCaps = Gst.Caps.from_string('video/x-raw')
  89. for idx, name in enumerate(self.names):
  90. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  91. if idx == self.sourceA:
  92. x = xa
  93. y = ycenter
  94. caps = scaleCaps
  95. alpha = 1
  96. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, x, y, alpha)
  97. self.log.debug('Setting Scaler %u to %u/%u', idx, targetWidth, targetHeight)
  98. elif idx == self.sourceB:
  99. x = xb
  100. y = ycenter
  101. caps = scaleCaps
  102. alpha = 1
  103. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, x, y, alpha)
  104. self.log.debug('Setting Scaler %u to %u/%u', idx, targetWidth, targetHeight)
  105. else:
  106. x = 0
  107. y = 0
  108. caps = noScaleCaps
  109. alpha = 0
  110. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, x, y, alpha)
  111. self.log.debug('Resetting Scaler %u to non-scaling', idx)
  112. mixerpad.set_property('alpha', alpha)
  113. mixerpad.set_property('xpos', x)
  114. mixerpad.set_property('ypos', y)
  115. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  116. capsfilter.set_property('caps', caps)
  117. def setVideoSourceA(self, source):
  118. # swap if required
  119. if self.sourceB == source:
  120. self.sourceB = self.sourceA
  121. self.sourceA = source
  122. self.updateMixerState()
  123. def setVideoSourceB(self, source):
  124. # swap if required
  125. if self.sourceA == source:
  126. self.sourceA = self.sourceB
  127. self.sourceB = source
  128. self.updateMixerState()
  129. def setCompositeMode(self, mode):
  130. self.compositeMode = mode
  131. self.updateMixerState()
  132. def on_eos(self, bus, message):
  133. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  134. def on_error(self, bus, message):
  135. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  136. (error, debug) = message.parse_error()
  137. self.log.debug('Error-Details: #%u: %s', error.code, debug)