aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/videomix.py
blob: 53e1d0327ade7b3eac6d5701e5e4d2f8d8025a35 (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. side_by_side_preview = 2
  10. class VideoMix(object):
  11. log = logging.getLogger('VideoMix')
  12. def __init__(self):
  13. self.caps = Config.get('mix', 'videocaps')
  14. self.names = Config.getlist('mix', 'sources')
  15. self.log.info('Configuring Mixer for %u Sources', len(self.names))
  16. pipeline = """
  17. videomixer name=mix !
  18. {caps} !
  19. queue !
  20. tee name=tee
  21. tee. ! queue ! intervideosink channel=video_mix_out
  22. """.format(
  23. caps=self.caps
  24. )
  25. if Config.getboolean('previews', 'enabled'):
  26. pipeline += """
  27. tee. ! queue ! intervideosink channel=video_mix_preview
  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('Binding Error & End-of-Stream-Signal on Mixing-Pipeline')
  44. self.mixingPipeline.bus.add_signal_watch()
  45. self.mixingPipeline.bus.connect("message::eos", self.on_eos)
  46. self.mixingPipeline.bus.connect("message::error", self.on_error)
  47. self.log.debug('Initializing Mixer-State')
  48. self.compositeMode = CompositeModes.fullscreen
  49. self.sourceA = 0
  50. self.sourceB = 1
  51. self.updateMixerState()
  52. self.log.debug('Launching Mixing-Pipeline')
  53. self.mixingPipeline.set_state(Gst.State.PLAYING)
  54. def getInputVideoSize(self):
  55. caps = Gst.Caps.from_string(self.caps)
  56. struct = caps.get_structure(0)
  57. _, width = struct.get_int('width')
  58. _, height = struct.get_int('height')
  59. return width, height
  60. def updateMixerState(self):
  61. if self.compositeMode == CompositeModes.fullscreen:
  62. self.updateMixerStateFullscreen()
  63. elif self.compositeMode == CompositeModes.side_by_side_equal:
  64. self.updateMixerStateSideBySideEqual()
  65. elif self.compositeMode == CompositeModes.side_by_side_preview:
  66. self.updateMixerStateSideBySidePreview()
  67. def updateMixerStateFullscreen(self):
  68. self.log.info('Updating Mixer-State for Fullscreen-Composition')
  69. noScaleCaps = Gst.Caps.from_string('video/x-raw')
  70. for idx, name in enumerate(self.names):
  71. alpha = int(idx == self.sourceA)
  72. self.log.debug('Setting Mixerpad %u to x/y=0 and alpha=%0.2f', idx, alpha)
  73. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  74. mixerpad.set_property('alpha', alpha )
  75. mixerpad.set_property('xpos', 0)
  76. mixerpad.set_property('ypos', 0)
  77. self.log.debug('Resetting Scaler %u to non-scaling', idx)
  78. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  79. capsfilter.set_property('caps', noScaleCaps)
  80. def updateMixerStateSideBySideEqual(self):
  81. self.log.info('Updating Mixer-State for Side-by-side-Equal-Composition')
  82. width, height = self.getInputVideoSize()
  83. self.log.debug('Video-Size parsed as %ux%u', width, height)
  84. try:
  85. gutter = Config.getint('side-by-side-equal', 'gutter')
  86. self.log.debug('Gutter configured to %u', gutter)
  87. except:
  88. gutter = int(width / 100)
  89. self.log.debug('Gutter calculated to %u', gutter)
  90. targetWidth = int((width - gutter) / 2)
  91. targetHeight = int(targetWidth / width * height)
  92. y = (height - targetHeight) / 2
  93. xa = 0
  94. xb = width - targetWidth
  95. scaleCaps = Gst.Caps.from_string('video/x-raw,width=%u,height=%u' % (targetWidth, targetHeight))
  96. noScaleCaps = Gst.Caps.from_string('video/x-raw')
  97. for idx, name in enumerate(self.names):
  98. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  99. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  100. if idx == self.sourceA:
  101. mixerpad.set_property('alpha', 1)
  102. mixerpad.set_property('xpos', xa)
  103. mixerpad.set_property('ypos', y)
  104. capsfilter.set_property('caps', scaleCaps)
  105. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, xa, y, 1)
  106. self.log.debug('Setting Scaler %u to %u/%u', idx, targetWidth, targetHeight)
  107. elif idx == self.sourceB:
  108. mixerpad.set_property('alpha', 1)
  109. mixerpad.set_property('xpos', xb)
  110. mixerpad.set_property('ypos', y)
  111. capsfilter.set_property('caps', scaleCaps)
  112. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, xb, y, 1)
  113. self.log.debug('Setting Scaler %u to %u/%u', idx, targetWidth, targetHeight)
  114. else:
  115. mixerpad.set_property('alpha', 0)
  116. mixerpad.set_property('xpos', 0)
  117. mixerpad.set_property('ypos', 0)
  118. capsfilter.set_property('caps', noScaleCaps)
  119. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, 0, 0, 0)
  120. self.log.debug('Resetting Scaler %u to non-scaling', idx)
  121. def updateMixerStateSideBySidePreview(self):
  122. self.log.info('Updating Mixer-State for Side-by-side-Preview-Composition')
  123. width, height = self.getInputVideoSize()
  124. self.log.debug('Video-Size parsed as %ux%u', width, height)
  125. try:
  126. asize = [int(i) for i in Config.get('side-by-side-preview', 'asize').split('x', 1)]
  127. self.log.debug('A-Video-Size configured to %ux%u', asize[0], asize[1])
  128. except:
  129. asize = [
  130. int(width / 1.25), # 80%
  131. int(height / 1.25) # 80%
  132. ]
  133. self.log.debug('A-Video-Size calculated to %ux%u', asize[0], asize[1])
  134. try:
  135. apos = [int(i) for i in Config.get('side-by-side-preview', 'apos').split('/', 1)]
  136. self.log.debug('B-Video-Position configured to %u/%u', apos[0], apos[1])
  137. except:
  138. apos = [
  139. int(width / 100), # 1%
  140. int(width / 100) # 1%
  141. ]
  142. self.log.debug('B-Video-Position calculated to %u/%u', apos[0], apos[1])
  143. try:
  144. bsize = [int(i) for i in Config.get('side-by-side-preview', 'bsize').split('x', 1)]
  145. self.log.debug('B-Video-Size configured to %ux%u', bsize[0], bsize[1])
  146. except:
  147. bsize = [
  148. int(width / 4), # 25%
  149. int(height / 4) # 25%
  150. ]
  151. self.log.debug('B-Video-Size calculated to %ux%u', bsize[0], bsize[1])
  152. try:
  153. bpos = [int(i) for i in Config.get('side-by-side-preview', 'bpos').split('/', 1)]
  154. self.log.debug('B-Video-Position configured to %u/%u', bpos[0], bpos[1])
  155. except:
  156. bpos = [
  157. width - int(width / 100) - bsize[0],
  158. height - int(width / 100) - bsize[1] # 1%
  159. ]
  160. self.log.debug('B-Video-Position calculated to %u/%u', bpos[0], bpos[1])
  161. aCaps = Gst.Caps.from_string('video/x-raw,width=%u,height=%u' % tuple(asize))
  162. bCaps = Gst.Caps.from_string('video/x-raw,width=%u,height=%u' % tuple(bsize))
  163. noScaleCaps = Gst.Caps.from_string('video/x-raw')
  164. for idx, name in enumerate(self.names):
  165. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  166. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  167. if idx == self.sourceA:
  168. mixerpad.set_property('alpha', 1)
  169. mixerpad.set_property('xpos', apos[1])
  170. mixerpad.set_property('ypos', apos[1])
  171. mixerpad.set_property('zorder', 0)
  172. capsfilter.set_property('caps', aCaps)
  173. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f, zorder=%u', idx, apos[0], apos[1], 1, 1)
  174. self.log.debug('Setting Scaler %u to %u/%u', idx, asize[0], asize[1])
  175. elif idx == self.sourceB:
  176. mixerpad.set_property('alpha', 1)
  177. mixerpad.set_property('xpos', bpos[0])
  178. mixerpad.set_property('ypos', bpos[1])
  179. mixerpad.set_property('zorder', 1)
  180. capsfilter.set_property('caps', bCaps)
  181. self.log.debug('Setting Mixerpad %u to x/y=%u/%u, alpha=%0.2f, zorder=%u', idx, bpos[0], bpos[1], 1, 0)
  182. self.log.debug('Setting Scaler %u to %u/%u', idx, bsize[0], bsize[1])
  183. else:
  184. mixerpad.set_property('alpha', 0)
  185. mixerpad.set_property('xpos', 0)
  186. mixerpad.set_property('ypos', 0)
  187. capsfilter.set_property('caps', noScaleCaps)
  188. self.log.debug('Setting Mixerpad %u to x/y=%u/%u and alpha=%0.2f', idx, 0, 0, 0)
  189. self.log.debug('Resetting Scaler %u to non-scaling', idx)
  190. def setVideoSourceA(self, source):
  191. # swap if required
  192. if self.sourceB == source:
  193. self.sourceB = self.sourceA
  194. self.sourceA = source
  195. self.updateMixerState()
  196. def setVideoSourceB(self, source):
  197. # swap if required
  198. if self.sourceA == source:
  199. self.sourceA = self.sourceB
  200. self.sourceB = source
  201. self.updateMixerState()
  202. def setCompositeMode(self, mode):
  203. self.compositeMode = mode
  204. self.updateMixerState()
  205. def on_eos(self, bus, message):
  206. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  207. def on_error(self, bus, message):
  208. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  209. (error, debug) = message.parse_error()
  210. self.log.debug('Error-Details: #%u: %s', error.code, debug)