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