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