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