summaryrefslogtreecommitdiff
path: root/voctocore/lib/videomix.py
blob: ac00d6d9878ce80e9a008cce51b384410d39cf98 (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} caps=video/x-raw !
  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,pixel-aspect-ratio=1/1' % (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,pixel-aspect-ratio=1/1' % tuple(pipsize))
  220. for idx, name in enumerate(self.names):
  221. pad = self.padState[idx]
  222. pad.reset()
  223. if idx == self.sourceA:
  224. pass
  225. elif idx == self.sourceB:
  226. pad.xpos, pad.ypos = pippos
  227. pad.zorder = 2
  228. pad.scaleCaps = scaleCaps
  229. else:
  230. pad.alpha = 0
  231. def getMixerpadAndCapsfilter(self, idx):
  232. # mixerpad 0 = background
  233. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % (idx+1))
  234. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  235. return mixerpad, capsfilter
  236. def applyMixerState(self):
  237. for idx, state in enumerate(self.padState):
  238. mixerpad, capsfilter = self.getMixerpadAndCapsfilter(idx)
  239. 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)
  240. self.log.debug('Reconfiguring Capsfilter %u to %s', idx, state.scaleCaps.to_string())
  241. mixerpad.set_property('xpos', state.xpos)
  242. mixerpad.set_property('ypos', state.ypos)
  243. mixerpad.set_property('alpha', state.alpha)
  244. mixerpad.set_property('zorder', state.zorder)
  245. capsfilter.set_property('caps', state.scaleCaps)
  246. def on_handoff(self, object, buffer):
  247. if self.padStateDirty:
  248. self.padStateDirty = False
  249. self.log.debug('[Streaming-Thread]: Pad-State is Dirty, applying new Mixer-State')
  250. self.applyMixerState()
  251. def on_eos(self, bus, message):
  252. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  253. def on_error(self, bus, message):
  254. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  255. (error, debug) = message.parse_error()
  256. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  257. def setVideoSourceA(self, source):
  258. # swap if required
  259. if self.sourceB == source:
  260. self.sourceB = self.sourceA
  261. self.sourceA = source
  262. self.recalculateMixerState()
  263. def getVideoSourceA(self):
  264. return self.sourceA
  265. def setVideoSourceB(self, source):
  266. # swap if required
  267. if self.sourceA == source:
  268. self.sourceA = self.sourceB
  269. self.sourceB = source
  270. self.recalculateMixerState()
  271. def getVideoSourceB(self):
  272. return self.sourceB
  273. def setCompositeMode(self, mode):
  274. self.compositeMode = mode
  275. self.recalculateMixerState()
  276. def getCompositeMode(self):
  277. return self.compositeMode