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