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