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