summaryrefslogtreecommitdiff
path: root/voctocore/lib/videomix.py
blob: bd4ca702a8773a351bd416a4d8eaebddc4ee16a2 (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. textoverlay halignment=left valignment=top ypad=125 text=VideoMix !
  19. timeoverlay halignment=left valignment=top ypad=125 xpad=400 !
  20. queue !
  21. tee name=tee
  22. tee. ! queue ! intervideosink channel=video_mix_out
  23. """.format(
  24. caps=self.caps
  25. )
  26. if Config.getboolean('previews', 'enabled'):
  27. pipeline += """
  28. tee. ! queue ! intervideosink channel=video_mix_preview
  29. """
  30. for idx, name in enumerate(self.names):
  31. pipeline += """
  32. intervideosrc channel=video_{name}_mixer !
  33. {caps} !
  34. videoscale !
  35. capsfilter name=caps_{idx} !
  36. mix.
  37. """.format(
  38. name=name,
  39. caps=self.caps,
  40. idx=idx
  41. )
  42. self.log.debug('Creating Mixing-Pipeline:\n%s', pipeline)
  43. self.mixingPipeline = Gst.parse_launch(pipeline)
  44. self.log.debug('Initializing Mixer-State')
  45. self.compositeMode = CompositeModes.fullscreen
  46. self.sourceA = 0
  47. self.sourceB = 1
  48. self.updateMixerState()
  49. self.log.debug('Launching Mixing-Pipeline')
  50. self.mixingPipeline.set_state(Gst.State.PLAYING)
  51. def updateMixerState(self):
  52. if self.compositeMode == CompositeModes.fullscreen:
  53. self.updateMixerStateFullscreen()
  54. if self.compositeMode == CompositeModes.side_by_side_equal:
  55. self.updateMixerStateSideBySideEqual()
  56. def updateMixerStateFullscreen(self):
  57. self.log.info('Updating Mixer-State for Fullscreen-Composition')
  58. noScaleCaps = Gst.Caps.from_string('video/x-raw')
  59. for idx, name in enumerate(self.names):
  60. alpha = int(idx == self.sourceA)
  61. self.log.debug('Setting Mixerpad %u to x/y=0 and alpha=%0.2f', idx, alpha)
  62. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  63. mixerpad.set_property('alpha', alpha )
  64. mixerpad.set_property('xpos', 0)
  65. mixerpad.set_property('ypos', 0)
  66. self.log.debug('Resetting Scaler %u to non-scaling', idx)
  67. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  68. capsfilter.set_property('caps', noScaleCaps)
  69. def getInputVideoSize(self):
  70. caps = Gst.Caps.from_string(self.caps)
  71. struct = caps.get_structure(0)
  72. _, width = struct.get_int('width')
  73. _, height = struct.get_int('height')
  74. return width, height
  75. def updateMixerStateSideBySideEqual(self):
  76. self.log.info('Updating Mixer-State for Side-by-side-Equal-Composition')
  77. width, height = self.getInputVideoSize()
  78. gutter = int(width / 100)
  79. self.log.debug('Video-Size parsed as %u/%u, Gutter calculated to %upx', width, height, gutter)
  80. targetWidth = int((width - gutter) / 2)
  81. targetHeight = int(targetWidth / width * height)
  82. ycenter = (height - targetHeight) / 2
  83. xa = 0
  84. xb = width - targetWidth
  85. scaleCaps = Gst.Caps.from_string('video/x-raw,width=%u,height=%u' % (targetWidth, targetHeight))
  86. noScaleCaps = Gst.Caps.from_string('video/x-raw')
  87. for idx, name in enumerate(self.names):
  88. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  89. if idx == self.sourceA:
  90. x = xa
  91. y = ycenter
  92. caps = scaleCaps
  93. alpha = 1
  94. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, x, y, alpha)
  95. self.log.debug('Setting Scaler %u to %u/%u', idx, targetWidth, targetHeight)
  96. elif idx == self.sourceB:
  97. x = xb
  98. y = ycenter
  99. caps = scaleCaps
  100. alpha = 1
  101. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, x, y, alpha)
  102. self.log.debug('Setting Scaler %u to %u/%u', idx, targetWidth, targetHeight)
  103. else:
  104. x = 0
  105. y = 0
  106. caps = noScaleCaps
  107. alpha = 0
  108. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, x, y, alpha)
  109. self.log.debug('Resetting Scaler %u to non-scaling', idx)
  110. mixerpad.set_property('alpha', alpha)
  111. mixerpad.set_property('xpos', x)
  112. mixerpad.set_property('ypos', y)
  113. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  114. capsfilter.set_property('caps', caps)
  115. def setVideoSourceA(self, source):
  116. # swap if required
  117. if self.sourceB == source:
  118. self.sourceB = self.sourceA
  119. self.sourceA = source
  120. self.updateMixerState()
  121. def setVideoSourceB(self, source):
  122. # swap if required
  123. if self.sourceA == source:
  124. self.sourceA = self.sourceB
  125. self.sourceB = source
  126. self.updateMixerState()
  127. def setCompositeMode(self, mode):
  128. self.compositeMode = mode
  129. self.updateMixerState()