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