summaryrefslogtreecommitdiff
path: root/voctocore/lib/videomix.py
blob: ebd60e6f40ee17844d4a4ec8601dd45162a7cf8f (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 PadState(object):
  12. noScaleCaps = Gst.Caps.from_string('video/x-raw')
  13. def __init__(self):
  14. self.reset()
  15. def reset(self):
  16. self.alpha = 1.0
  17. self.xpos = 0
  18. self.ypos = 0
  19. self.zorder = 1
  20. self.scaleCaps = PadState.noScaleCaps
  21. class VideoMix(object):
  22. log = logging.getLogger('VideoMix')
  23. def __init__(self):
  24. self.caps = Config.get('mix', 'videocaps')
  25. self.names = Config.getlist('mix', 'sources')
  26. self.log.info('Configuring Mixer for %u Sources', len(self.names))
  27. pipeline = """
  28. videomixer name=mix !
  29. {caps} !
  30. identity name=sig !
  31. queue !
  32. tee name=tee
  33. intervideosrc channel=video_background !
  34. {caps} !
  35. mix.
  36. tee. ! queue ! intervideosink channel=video_mix_out
  37. """.format(
  38. caps=self.caps
  39. )
  40. if Config.getboolean('previews', 'enabled'):
  41. pipeline += """
  42. tee. ! queue ! intervideosink channel=video_mix_preview
  43. """
  44. for idx, name in enumerate(self.names):
  45. pipeline += """
  46. intervideosrc channel=video_{name}_mixer !
  47. {caps} !
  48. videoscale !
  49. capsfilter name=caps_{idx} !
  50. mix.
  51. """.format(
  52. name=name,
  53. caps=self.caps,
  54. idx=idx
  55. )
  56. self.log.debug('Creating Mixing-Pipeline:\n%s', pipeline)
  57. self.mixingPipeline = Gst.parse_launch(pipeline)
  58. self.log.debug('Binding Error & End-of-Stream-Signal on Mixing-Pipeline')
  59. self.mixingPipeline.bus.add_signal_watch()
  60. self.mixingPipeline.bus.connect("message::eos", self.on_eos)
  61. self.mixingPipeline.bus.connect("message::error", self.on_error)
  62. self.log.debug('Binding Handoff-Handler for Synchronus mixer manipulation')
  63. sig = self.mixingPipeline.get_by_name('sig')
  64. sig.connect('handoff', self.on_handoff)
  65. self.padStateDirty = False
  66. self.padState = list()
  67. for idx, name in enumerate(self.names):
  68. self.padState.append(PadState())
  69. self.log.debug('Initializing Mixer-State')
  70. self.compositeMode = CompositeModes.fullscreen
  71. self.sourceA = 0
  72. self.sourceB = 1
  73. self.recalculateMixerState()
  74. self.applyMixerState()
  75. bgMixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_0')
  76. bgMixerpad.set_property('zorder', 0)
  77. self.log.debug('Launching Mixing-Pipeline')
  78. self.mixingPipeline.set_state(Gst.State.PLAYING)
  79. def getInputVideoSize(self):
  80. caps = Gst.Caps.from_string(self.caps)
  81. struct = caps.get_structure(0)
  82. _, width = struct.get_int('width')
  83. _, height = struct.get_int('height')
  84. return width, height
  85. def recalculateMixerState(self):
  86. if self.compositeMode == CompositeModes.fullscreen:
  87. self.recalculateMixerStateFullscreen()
  88. elif self.compositeMode == CompositeModes.side_by_side_equal:
  89. self.recalculateMixerStateSideBySideEqual()
  90. elif self.compositeMode == CompositeModes.side_by_side_preview:
  91. self.recalculateMixerStateSideBySidePreview()
  92. elif self.compositeMode == CompositeModes.picture_in_picture:
  93. self.recalculateMixerStatePictureInPicture()
  94. self.log.debug('Marking Pad-State as Dirty')
  95. self.padStateDirty = True
  96. def recalculateMixerStateFullscreen(self):
  97. self.log.info('Updating Mixer-State for Fullscreen-Composition')
  98. for idx, name in enumerate(self.names):
  99. pad = self.padState[idx]
  100. pad.reset()
  101. pad.alpha = float(idx == self.sourceA)
  102. def recalculateMixerStateSideBySideEqual(self):
  103. self.log.info('Updating Mixer-State for Side-by-side-Equal-Composition')
  104. width, height = self.getInputVideoSize()
  105. self.log.debug('Video-Size parsed as %ux%u', width, height)
  106. try:
  107. gutter = Config.getint('side-by-side-equal', 'gutter')
  108. self.log.debug('Gutter configured to %u', gutter)
  109. except:
  110. gutter = int(width / 100)
  111. self.log.debug('Gutter calculated to %u', gutter)
  112. targetWidth = int((width - gutter) / 2)
  113. targetHeight = int(targetWidth / width * height)
  114. try:
  115. y = Config.getint('side-by-side-equal', 'ypos')
  116. self.log.debug('Y-Pos configured to %u', y)
  117. except:
  118. y = (height - targetHeight) / 2
  119. self.log.debug('Y-Pos calculated to %u', y)
  120. xa = 0
  121. xb = width - targetWidth
  122. scaleCaps = Gst.Caps.from_string('video/x-raw,width=%u,height=%u' % (targetWidth, targetHeight))
  123. for idx, name in enumerate(self.names):
  124. pad = self.padState[idx]
  125. pad.reset()
  126. if idx == self.sourceA:
  127. pad.xpos = xa
  128. pad.ypos = y
  129. pad.zorder = 1
  130. pad.scaleCaps = scaleCaps
  131. elif idx == self.sourceB:
  132. pad.xpos = xb
  133. pad.ypos = y
  134. pad.zorder = 2
  135. pad.scaleCaps = scaleCaps
  136. else:
  137. pad.alpha = 0
  138. def recalculateMixerStateSideBySidePreview(self):
  139. self.log.info('Updating Mixer-State for Side-by-side-Preview-Composition')
  140. width, height = self.getInputVideoSize()
  141. self.log.debug('Video-Size parsed as %ux%u', width, height)
  142. try:
  143. asize = [int(i) for i in Config.get('side-by-side-preview', 'asize').split('x', 1)]
  144. self.log.debug('A-Video-Size configured to %ux%u', asize[0], asize[1])
  145. except:
  146. asize = [
  147. int(width / 1.25), # 80%
  148. int(height / 1.25) # 80%
  149. ]
  150. self.log.debug('A-Video-Size calculated to %ux%u', asize[0], asize[1])
  151. try:
  152. apos = [int(i) for i in Config.get('side-by-side-preview', 'apos').split('/', 1)]
  153. self.log.debug('B-Video-Position configured to %u/%u', apos[0], apos[1])
  154. except:
  155. apos = [
  156. int(width / 100), # 1%
  157. int(width / 100) # 1%
  158. ]
  159. self.log.debug('B-Video-Position calculated to %u/%u', apos[0], apos[1])
  160. try:
  161. bsize = [int(i) for i in Config.get('side-by-side-preview', 'bsize').split('x', 1)]
  162. self.log.debug('B-Video-Size configured to %ux%u', bsize[0], bsize[1])
  163. except:
  164. bsize = [
  165. int(width / 4), # 25%
  166. int(height / 4) # 25%
  167. ]
  168. self.log.debug('B-Video-Size calculated to %ux%u', bsize[0], bsize[1])
  169. try:
  170. bpos = [int(i) for i in Config.get('side-by-side-preview', 'bpos').split('/', 1)]
  171. self.log.debug('B-Video-Position configured to %u/%u', bpos[0], bpos[1])
  172. except:
  173. bpos = [
  174. width - int(width / 100) - bsize[0],
  175. height - int(width / 100) - bsize[1] # 1%
  176. ]
  177. self.log.debug('B-Video-Position calculated to %u/%u', bpos[0], bpos[1])
  178. aCaps = Gst.Caps.from_string('video/x-raw,width=%u,height=%u' % tuple(asize))
  179. bCaps = Gst.Caps.from_string('video/x-raw,width=%u,height=%u' % tuple(bsize))
  180. for idx, name in enumerate(self.names):
  181. pad = self.padState[idx]
  182. pad.reset()
  183. if idx == self.sourceA:
  184. pad.xpos, pad.ypos = apos
  185. pad.zorder = 1
  186. pad.scaleCaps = aCaps
  187. elif idx == self.sourceB:
  188. pad.xpos, pad.ypos = bpos
  189. pad.zorder = 2
  190. pad.scaleCaps = bCaps
  191. else:
  192. pad.alpha = 0
  193. def recalculateMixerStatePictureInPicture(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. pad = self.padState[idx]
  219. pad.reset()
  220. if idx == self.sourceA:
  221. pass
  222. elif idx == self.sourceB:
  223. pad.xpos, pad.ypos = pippos
  224. pad.zorder = 2
  225. pad.scaleCaps = scaleCaps
  226. else:
  227. pad.alpha = 0
  228. def getMixerpadAndCapsfilter(self, idx):
  229. # mixerpad 0 = background
  230. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % (idx+1))
  231. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  232. return mixerpad, capsfilter
  233. def applyMixerState(self):
  234. for idx, state in enumerate(self.padState):
  235. mixerpad, capsfilter = self.getMixerpadAndCapsfilter(idx)
  236. self.log.debug('Reconfiguring Mixerpad %u to x/y=%u/%u, alpha=%0.2f, zorder=%u', idx, state.xpos, state.ypos, state.alpha, state.zorder)
  237. self.log.debug('Reconfiguring Capsfilter %u to %s', idx, state.scaleCaps.to_string())
  238. mixerpad.set_property('xpos', state.xpos)
  239. mixerpad.set_property('ypos', state.ypos)
  240. mixerpad.set_property('alpha', state.alpha)
  241. mixerpad.set_property('zorder', state.zorder)
  242. capsfilter.set_property('caps', state.scaleCaps)
  243. def on_handoff(self, object, buffer):
  244. if self.padStateDirty:
  245. self.padStateDirty = False
  246. self.log.debug('[Streaming-Thread]: Pad-State is Dirty, applying new Mixer-State')
  247. self.applyMixerState()
  248. def on_eos(self, bus, message):
  249. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  250. def on_error(self, bus, message):
  251. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  252. (error, debug) = message.parse_error()
  253. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  254. def setVideoSourceA(self, source):
  255. # swap if required
  256. if self.sourceB == source:
  257. self.sourceB = self.sourceA
  258. self.sourceA = source
  259. self.recalculateMixerState()
  260. def setVideoSourceB(self, source):
  261. # swap if required
  262. if self.sourceA == source:
  263. self.sourceA = self.sourceB
  264. self.sourceB = source
  265. self.recalculateMixerState()
  266. def setCompositeMode(self, mode):
  267. self.compositeMode = mode
  268. self.recalculateMixerState()