summaryrefslogtreecommitdiff
path: root/voctocore/lib/videomix.py
blob: f45b7a3ab502d814d1cc8cbbd30d3f7a6f6fd77a (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=mixer_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. y = (height - targetHeight) / 2
  115. xa = 0
  116. xb = width - targetWidth
  117. scaleCaps = Gst.Caps.from_string('video/x-raw,width=%u,height=%u' % (targetWidth, targetHeight))
  118. for idx, name in enumerate(self.names):
  119. pad = self.padState[idx]
  120. pad.reset()
  121. if idx == self.sourceA:
  122. pad.xpos = xa
  123. pad.ypos = y
  124. pad.zorder = 1
  125. pad.scaleCaps = scaleCaps
  126. elif idx == self.sourceB:
  127. pad.xpos = xb
  128. pad.ypos = y
  129. pad.zorder = 2
  130. pad.scaleCaps = scaleCaps
  131. else:
  132. pad.alpha = 0
  133. def recalculateMixerStateSideBySidePreview(self):
  134. self.log.info('Updating Mixer-State for Side-by-side-Preview-Composition')
  135. width, height = self.getInputVideoSize()
  136. self.log.debug('Video-Size parsed as %ux%u', width, height)
  137. try:
  138. asize = [int(i) for i in Config.get('side-by-side-preview', 'asize').split('x', 1)]
  139. self.log.debug('A-Video-Size configured to %ux%u', asize[0], asize[1])
  140. except:
  141. asize = [
  142. int(width / 1.25), # 80%
  143. int(height / 1.25) # 80%
  144. ]
  145. self.log.debug('A-Video-Size calculated to %ux%u', asize[0], asize[1])
  146. try:
  147. apos = [int(i) for i in Config.get('side-by-side-preview', 'apos').split('/', 1)]
  148. self.log.debug('B-Video-Position configured to %u/%u', apos[0], apos[1])
  149. except:
  150. apos = [
  151. int(width / 100), # 1%
  152. int(width / 100) # 1%
  153. ]
  154. self.log.debug('B-Video-Position calculated to %u/%u', apos[0], apos[1])
  155. try:
  156. bsize = [int(i) for i in Config.get('side-by-side-preview', 'bsize').split('x', 1)]
  157. self.log.debug('B-Video-Size configured to %ux%u', bsize[0], bsize[1])
  158. except:
  159. bsize = [
  160. int(width / 4), # 25%
  161. int(height / 4) # 25%
  162. ]
  163. self.log.debug('B-Video-Size calculated to %ux%u', bsize[0], bsize[1])
  164. try:
  165. bpos = [int(i) for i in Config.get('side-by-side-preview', 'bpos').split('/', 1)]
  166. self.log.debug('B-Video-Position configured to %u/%u', bpos[0], bpos[1])
  167. except:
  168. bpos = [
  169. width - int(width / 100) - bsize[0],
  170. height - int(width / 100) - bsize[1] # 1%
  171. ]
  172. self.log.debug('B-Video-Position calculated to %u/%u', bpos[0], bpos[1])
  173. aCaps = Gst.Caps.from_string('video/x-raw,width=%u,height=%u' % tuple(asize))
  174. bCaps = Gst.Caps.from_string('video/x-raw,width=%u,height=%u' % tuple(bsize))
  175. for idx, name in enumerate(self.names):
  176. pad = self.padState[idx]
  177. pad.reset()
  178. if idx == self.sourceA:
  179. pad.xpos, pad.ypos = apos
  180. pad.zorder = 1
  181. pad.scaleCaps = aCaps
  182. elif idx == self.sourceB:
  183. pad.xpos, pad.ypos = bpos
  184. pad.zorder = 2
  185. pad.scaleCaps = bCaps
  186. else:
  187. pad.alpha = 0
  188. def recalculateMixerStatePictureInPicture(self):
  189. self.log.info('Updating Mixer-State for Picture-in-Picture-Composition')
  190. width, height = self.getInputVideoSize()
  191. self.log.debug('Video-Size parsed as %ux%u', width, height)
  192. try:
  193. pipsize = [int(i) for i in Config.get('picture-in-picture', 'pipsize').split('x', 1)]
  194. self.log.debug('PIP-Size configured to %ux%u', pipsize[0], pipsize[1])
  195. except:
  196. pipsize = [
  197. int(width / 4), # 25%
  198. int(height / 4) # 25%
  199. ]
  200. self.log.debug('PIP-Size calculated to %ux%u', pipsize[0], pipsize[1])
  201. try:
  202. pippos = [int(i) for i in Config.get('picture-in-picture', 'pippos').split('/', 1)]
  203. self.log.debug('PIP-Position configured to %u/%u', pippos[0], pippos[1])
  204. except:
  205. pippos = [
  206. width - pipsize[0] - int(width / 100), # 1%
  207. height - pipsize[1] -int(width / 100) # 1%
  208. ]
  209. self.log.debug('PIP-Position calculated to %u/%u', pippos[0], pippos[1])
  210. scaleCaps = Gst.Caps.from_string('video/x-raw,width=%u,height=%u' % tuple(pipsize))
  211. noScaleCaps = Gst.Caps.from_string('video/x-raw')
  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.zorder = 2
  220. pad.scaleCaps = scaleCaps
  221. else:
  222. pad.alpha = 0
  223. def getMixerpadAndCapsfilter(self, idx):
  224. # mixerpad 0 = background
  225. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % (idx+1))
  226. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  227. return mixerpad, capsfilter
  228. def applyMixerState(self):
  229. for idx, state in enumerate(self.padState):
  230. mixerpad, capsfilter = self.getMixerpadAndCapsfilter(idx)
  231. 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)
  232. self.log.debug('Reconfiguring Capsfilter %u to %s', idx, state.scaleCaps.to_string())
  233. mixerpad.set_property('xpos', state.xpos)
  234. mixerpad.set_property('ypos', state.ypos)
  235. mixerpad.set_property('alpha', state.alpha)
  236. mixerpad.set_property('zorder', state.zorder)
  237. capsfilter.set_property('caps', state.scaleCaps)
  238. def on_handoff(self, object, buffer):
  239. if self.padStateDirty:
  240. self.padStateDirty = False
  241. self.log.debug('[Streaming-Thread]: Pad-State is Dirty, applying new Mixer-State')
  242. self.applyMixerState()
  243. def on_eos(self, bus, message):
  244. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  245. def on_error(self, bus, message):
  246. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  247. (error, debug) = message.parse_error()
  248. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  249. def setVideoSourceA(self, source):
  250. # swap if required
  251. if self.sourceB == source:
  252. self.sourceB = self.sourceA
  253. self.sourceA = source
  254. self.recalculateMixerState()
  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 setCompositeMode(self, mode):
  262. self.compositeMode = mode
  263. self.recalculateMixerState()