summaryrefslogtreecommitdiff
path: root/voctocore/lib/videomix.py
blob: aa2bedc195103b13eca561bcc61542a5744ed8a0 (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.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. {caps} !
  53. mix.
  54. """.format(
  55. name=name,
  56. caps=self.caps,
  57. idx=idx
  58. )
  59. self.log.debug('Creating Mixing-Pipeline:\n%s', pipeline)
  60. self.mixingPipeline = Gst.parse_launch(pipeline)
  61. self.mixingPipeline.use_clock(Clock)
  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. self.log.debug('Video-Size calculated to %ux%u', targetWidth, targetHeight)
  119. xa = 0
  120. xb = width - targetWidth
  121. y = (height - targetHeight) / 2
  122. try:
  123. ya = Config.getint('side-by-side-equal', 'atop')
  124. self.log.debug('A-Video Y-Pos configured to %u', ya)
  125. except:
  126. ya = y
  127. self.log.debug('A-Video Y-Pos calculated to %u', ya)
  128. try:
  129. yb = Config.getint('side-by-side-equal', 'btop')
  130. self.log.debug('B-Video Y-Pos configured to %u', yb)
  131. except:
  132. yb = y
  133. self.log.debug('B-Video Y-Pos calculated to %u', yb)
  134. for idx, name in enumerate(self.names):
  135. pad = self.padState[idx]
  136. pad.reset()
  137. pad.width = targetWidth
  138. pad.height = targetHeight
  139. if idx == self.sourceA:
  140. pad.xpos = xa
  141. pad.ypos = ya
  142. pad.zorder = 1
  143. elif idx == self.sourceB:
  144. pad.xpos = xb
  145. pad.ypos = yb
  146. pad.zorder = 2
  147. else:
  148. pad.alpha = 0
  149. def recalculateMixerStateSideBySidePreview(self):
  150. self.log.info('Updating Mixer-State for Side-by-side-Preview-Composition')
  151. width, height = self.getInputVideoSize()
  152. self.log.debug('Video-Size parsed as %ux%u', width, height)
  153. try:
  154. asize = [int(i) for i in Config.get('side-by-side-preview', 'asize').split('x', 1)]
  155. self.log.debug('A-Video-Size configured to %ux%u', asize[0], asize[1])
  156. except:
  157. asize = [
  158. int(width / 1.25), # 80%
  159. int(height / 1.25) # 80%
  160. ]
  161. self.log.debug('A-Video-Size calculated to %ux%u', asize[0], asize[1])
  162. try:
  163. apos = [int(i) for i in Config.get('side-by-side-preview', 'apos').split('/', 1)]
  164. self.log.debug('B-Video-Position configured to %u/%u', apos[0], apos[1])
  165. except:
  166. apos = [
  167. int(width / 100), # 1%
  168. int(width / 100) # 1%
  169. ]
  170. self.log.debug('B-Video-Position calculated to %u/%u', apos[0], apos[1])
  171. try:
  172. bsize = [int(i) for i in Config.get('side-by-side-preview', 'bsize').split('x', 1)]
  173. self.log.debug('B-Video-Size configured to %ux%u', bsize[0], bsize[1])
  174. except:
  175. bsize = [
  176. int(width / 4), # 25%
  177. int(height / 4) # 25%
  178. ]
  179. self.log.debug('B-Video-Size calculated to %ux%u', bsize[0], bsize[1])
  180. try:
  181. bpos = [int(i) for i in Config.get('side-by-side-preview', 'bpos').split('/', 1)]
  182. self.log.debug('B-Video-Position configured to %u/%u', bpos[0], bpos[1])
  183. except:
  184. bpos = [
  185. width - int(width / 100) - bsize[0],
  186. height - int(width / 100) - bsize[1] # 1%
  187. ]
  188. self.log.debug('B-Video-Position calculated to %u/%u', bpos[0], bpos[1])
  189. for idx, name in enumerate(self.names):
  190. pad = self.padState[idx]
  191. pad.reset()
  192. if idx == self.sourceA:
  193. pad.xpos, pad.ypos = apos
  194. pad.width, pad.height = asize
  195. pad.zorder = 1
  196. elif idx == self.sourceB:
  197. pad.xpos, pad.ypos = bpos
  198. pad.width, pad.height = bsize
  199. pad.zorder = 2
  200. else:
  201. pad.alpha = 0
  202. def recalculateMixerStatePictureInPicture(self):
  203. self.log.info('Updating Mixer-State for Picture-in-Picture-Composition')
  204. width, height = self.getInputVideoSize()
  205. self.log.debug('Video-Size parsed as %ux%u', width, height)
  206. try:
  207. pipsize = [int(i) for i in Config.get('picture-in-picture', 'pipsize').split('x', 1)]
  208. self.log.debug('PIP-Size configured to %ux%u', pipsize[0], pipsize[1])
  209. except:
  210. pipsize = [
  211. int(width / 4), # 25%
  212. int(height / 4) # 25%
  213. ]
  214. self.log.debug('PIP-Size calculated to %ux%u', pipsize[0], pipsize[1])
  215. try:
  216. pippos = [int(i) for i in Config.get('picture-in-picture', 'pippos').split('/', 1)]
  217. self.log.debug('PIP-Position configured to %u/%u', pippos[0], pippos[1])
  218. except:
  219. pippos = [
  220. width - pipsize[0] - int(width / 100), # 1%
  221. height - pipsize[1] -int(width / 100) # 1%
  222. ]
  223. self.log.debug('PIP-Position calculated to %u/%u', pippos[0], pippos[1])
  224. for idx, name in enumerate(self.names):
  225. pad = self.padState[idx]
  226. pad.reset()
  227. if idx == self.sourceA:
  228. pass
  229. elif idx == self.sourceB:
  230. pad.xpos, pad.ypos = pippos
  231. pad.width, pad.height = pipsize
  232. pad.zorder = 2
  233. else:
  234. pad.alpha = 0
  235. def applyMixerState(self):
  236. for idx, state in enumerate(self.padState):
  237. # mixerpad 0 = background
  238. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % (idx+1))
  239. self.log.debug('Reconfiguring Mixerpad %u to x/y=%u/%u, w/h=%u/%u alpha=%0.2f, zorder=%u', \
  240. idx, state.xpos, state.ypos, state.width, state.height, state.alpha, state.zorder)
  241. mixerpad.set_property('xpos', state.xpos)
  242. mixerpad.set_property('ypos', state.ypos)
  243. mixerpad.set_property('width', state.width)
  244. mixerpad.set_property('height', state.height)
  245. mixerpad.set_property('alpha', state.alpha)
  246. mixerpad.set_property('zorder', state.zorder)
  247. def selectCompositeModeDefaultSources(self):
  248. sectionNames = {
  249. CompositeModes.fullscreen: 'fullscreen',
  250. CompositeModes.side_by_side_equal: 'side-by-side-equal',
  251. CompositeModes.side_by_side_preview: 'side-by-side-preview',
  252. CompositeModes.picture_in_picture: 'picture-in-picture'
  253. }
  254. compositeModeName = self.compositeMode.name
  255. sectionName = sectionNames[self.compositeMode]
  256. try:
  257. defSource = Config.get(sectionName, 'default-a')
  258. self.setVideoSourceA(self.names.index(defSource))
  259. self.log.info('Changing sourceA to default of Mode %s: %s', compositeModeName, defSource)
  260. except Exception as e:
  261. print(e)
  262. pass
  263. try:
  264. defSource = Config.get(sectionName, 'default-b')
  265. self.setVideoSourceB(self.names.index(defSource))
  266. self.log.info('Changing sourceB to default of Mode %s: %s', compositeModeName, defSource)
  267. except Exception as e:
  268. print(e)
  269. pass
  270. def on_handoff(self, object, buffer):
  271. if self.padStateDirty:
  272. self.padStateDirty = False
  273. self.log.debug('[Streaming-Thread]: Pad-State is Dirty, applying new Mixer-State')
  274. self.applyMixerState()
  275. def on_eos(self, bus, message):
  276. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  277. def on_error(self, bus, message):
  278. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  279. (error, debug) = message.parse_error()
  280. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  281. def setVideoSourceA(self, source):
  282. # swap if required
  283. if self.sourceB == source:
  284. self.sourceB = self.sourceA
  285. self.sourceA = source
  286. self.recalculateMixerState()
  287. def getVideoSourceA(self):
  288. return self.sourceA
  289. def setVideoSourceB(self, source):
  290. # swap if required
  291. if self.sourceA == source:
  292. self.sourceA = self.sourceB
  293. self.sourceB = source
  294. self.recalculateMixerState()
  295. def getVideoSourceB(self):
  296. return self.sourceB
  297. def setCompositeMode(self, mode):
  298. self.compositeMode = mode
  299. self.selectCompositeModeDefaultSources()
  300. self.recalculateMixerState()
  301. def getCompositeMode(self):
  302. return self.compositeMode