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