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