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