summaryrefslogtreecommitdiff
path: root/voctocore/lib/videomix.py
blob: 73efa0437bd53a0fc362ecf974977cb408ad15de (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('Binding Error & End-of-Stream-Signal on Mixing-Pipeline')
  43. self.mixingPipeline.bus.add_signal_watch()
  44. self.mixingPipeline.bus.connect("message::eos", self.on_eos)
  45. self.mixingPipeline.bus.connect("message::error", self.on_error)
  46. self.log.debug('Initializing Mixer-State')
  47. self.compositeMode = CompositeModes.fullscreen
  48. self.sourceA = 0
  49. self.sourceB = 1
  50. self.updateMixerState()
  51. self.log.debug('Launching Mixing-Pipeline')
  52. self.mixingPipeline.set_state(Gst.State.PLAYING)
  53. def getInputVideoSize(self):
  54. caps = Gst.Caps.from_string(self.caps)
  55. struct = caps.get_structure(0)
  56. _, width = struct.get_int('width')
  57. _, height = struct.get_int('height')
  58. return width, height
  59. def updateMixerState(self):
  60. if self.compositeMode == CompositeModes.fullscreen:
  61. self.updateMixerStateFullscreen()
  62. if self.compositeMode == CompositeModes.side_by_side_equal:
  63. self.updateMixerStateSideBySideEqual()
  64. def updateMixerStateFullscreen(self):
  65. self.log.info('Updating Mixer-State for Fullscreen-Composition')
  66. noScaleCaps = Gst.Caps.from_string('video/x-raw')
  67. for idx, name in enumerate(self.names):
  68. alpha = int(idx == self.sourceA)
  69. self.log.debug('Setting Mixerpad %u to x/y=0 and alpha=%0.2f', idx, alpha)
  70. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  71. mixerpad.set_property('alpha', alpha )
  72. mixerpad.set_property('xpos', 0)
  73. mixerpad.set_property('ypos', 0)
  74. self.log.debug('Resetting Scaler %u to non-scaling', idx)
  75. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  76. capsfilter.set_property('caps', noScaleCaps)
  77. def updateMixerStateSideBySideEqual(self):
  78. self.log.info('Updating Mixer-State for Side-by-side-Equal-Composition')
  79. width, height = self.getInputVideoSize()
  80. gutter = int(width / 100)
  81. self.log.debug('Video-Size parsed as %u/%u, Gutter calculated to %upx', width, height, gutter)
  82. targetWidth = int((width - gutter) / 2)
  83. targetHeight = int(targetWidth / width * height)
  84. y = (height - targetHeight) / 2
  85. xa = 0
  86. xb = width - targetWidth
  87. scaleCaps = Gst.Caps.from_string('video/x-raw,width=%u,height=%u' % (targetWidth, targetHeight))
  88. noScaleCaps = Gst.Caps.from_string('video/x-raw')
  89. for idx, name in enumerate(self.names):
  90. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  91. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  92. if idx == self.sourceA:
  93. mixerpad.set_property('alpha', 1)
  94. mixerpad.set_property('xpos', xa)
  95. mixerpad.set_property('ypos', y)
  96. capsfilter.set_property('caps', scaleCaps)
  97. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, xa, y, 1)
  98. self.log.debug('Setting Scaler %u to %u/%u', idx, targetWidth, targetHeight)
  99. elif idx == self.sourceB:
  100. mixerpad.set_property('alpha', 1)
  101. mixerpad.set_property('xpos', xb)
  102. mixerpad.set_property('ypos', y)
  103. capsfilter.set_property('caps', scaleCaps)
  104. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, xb, y, 1)
  105. self.log.debug('Setting Scaler %u to %u/%u', idx, targetWidth, targetHeight)
  106. else:
  107. mixerpad.set_property('alpha', 0)
  108. mixerpad.set_property('xpos', 0)
  109. mixerpad.set_property('ypos', 0)
  110. capsfilter.set_property('caps', noScaleCaps)
  111. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, 0, 0, 0)
  112. self.log.debug('Resetting Scaler %u to non-scaling', idx)
  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()
  128. def on_eos(self, bus, message):
  129. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  130. def on_error(self, bus, message):
  131. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  132. (error, debug) = message.parse_error()
  133. self.log.debug('Error-Details: #%u: %s', error.code, debug)