aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/videomix.py
blob: 4ab740fda89f36cd3964fa18283d11333a395083 (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 '
  63. '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 '
  68. 'Synchronus mixer manipulation')
  69. sig = self.mixingPipeline.get_by_name('sig')
  70. sig.connect('handoff', self.on_handoff)
  71. self.padStateDirty = False
  72. self.padState = list()
  73. for idx, name in enumerate(self.names):
  74. self.padState.append(PadState())
  75. self.log.debug('Initializing Mixer-State')
  76. self.compositeMode = CompositeModes.fullscreen
  77. self.sourceA = 0
  78. self.sourceB = 1
  79. self.recalculateMixerState()
  80. self.applyMixerState()
  81. bgMixerpad = (self.mixingPipeline.get_by_name('mix')
  82. .get_static_pad('sink_0'))
  83. bgMixerpad.set_property('zorder', 0)
  84. self.log.debug('Launching Mixing-Pipeline')
  85. self.mixingPipeline.set_state(Gst.State.PLAYING)
  86. def getInputVideoSize(self):
  87. caps = Gst.Caps.from_string(self.caps)
  88. struct = caps.get_structure(0)
  89. _, width = struct.get_int('width')
  90. _, height = struct.get_int('height')
  91. return width, height
  92. def recalculateMixerState(self):
  93. if self.compositeMode == CompositeModes.fullscreen:
  94. self.recalculateMixerStateFullscreen()
  95. elif self.compositeMode == CompositeModes.side_by_side_equal:
  96. self.recalculateMixerStateSideBySideEqual()
  97. elif self.compositeMode == CompositeModes.side_by_side_preview:
  98. self.recalculateMixerStateSideBySidePreview()
  99. elif self.compositeMode == CompositeModes.picture_in_picture:
  100. self.recalculateMixerStatePictureInPicture()
  101. self.log.debug('Marking Pad-State as Dirty')
  102. self.padStateDirty = True
  103. def recalculateMixerStateFullscreen(self):
  104. self.log.info('Updating Mixer-State for Fullscreen-Composition')
  105. for idx, name in enumerate(self.names):
  106. pad = self.padState[idx]
  107. pad.reset()
  108. pad.alpha = float(idx == self.sourceA)
  109. def recalculateMixerStateSideBySideEqual(self):
  110. self.log.info('Updating Mixer-State for '
  111. 'Side-by-side-Equal-Composition')
  112. width, height = self.getInputVideoSize()
  113. self.log.debug('Video-Size parsed as %ux%u', width, height)
  114. try:
  115. gutter = Config.getint('side-by-side-equal', 'gutter')
  116. self.log.debug('Gutter configured to %u', gutter)
  117. except:
  118. gutter = int(width / 100)
  119. self.log.debug('Gutter calculated to %u', gutter)
  120. targetWidth = int((width - gutter) / 2)
  121. targetHeight = int(targetWidth / width * height)
  122. self.log.debug('Video-Size calculated to %ux%u',
  123. targetWidth, targetHeight)
  124. xa = 0
  125. xb = width - targetWidth
  126. y = (height - targetHeight) / 2
  127. try:
  128. ya = Config.getint('side-by-side-equal', 'atop')
  129. self.log.debug('A-Video Y-Pos configured to %u', ya)
  130. except:
  131. ya = y
  132. self.log.debug('A-Video Y-Pos calculated to %u', ya)
  133. try:
  134. yb = Config.getint('side-by-side-equal', 'btop')
  135. self.log.debug('B-Video Y-Pos configured to %u', yb)
  136. except:
  137. yb = y
  138. self.log.debug('B-Video Y-Pos calculated to %u', yb)
  139. for idx, name in enumerate(self.names):
  140. pad = self.padState[idx]
  141. pad.reset()
  142. pad.width = targetWidth
  143. pad.height = targetHeight
  144. if idx == self.sourceA:
  145. pad.xpos = xa
  146. pad.ypos = ya
  147. pad.zorder = 1
  148. elif idx == self.sourceB:
  149. pad.xpos = xb
  150. pad.ypos = yb
  151. pad.zorder = 2
  152. else:
  153. pad.alpha = 0
  154. def recalculateMixerStateSideBySidePreview(self):
  155. self.log.info('Updating Mixer-State for '
  156. 'Side-by-side-Preview-Composition')
  157. width, height = self.getInputVideoSize()
  158. self.log.debug('Video-Size parsed as %ux%u', width, height)
  159. try:
  160. asize = [int(i) for i in Config.get('side-by-side-preview',
  161. 'asize').split('x', 1)]
  162. self.log.debug('A-Video-Size configured to %ux%u',
  163. asize[0], asize[1])
  164. except:
  165. asize = [
  166. int(width / 1.25), # 80%
  167. int(height / 1.25) # 80%
  168. ]
  169. self.log.debug('A-Video-Size calculated to %ux%u',
  170. asize[0], asize[1])
  171. try:
  172. apos = [int(i) for i in Config.get('side-by-side-preview',
  173. 'apos').split('/', 1)]
  174. self.log.debug('B-Video-Position configured to %u/%u',
  175. apos[0], apos[1])
  176. except:
  177. apos = [
  178. int(width / 100), # 1%
  179. int(width / 100) # 1%
  180. ]
  181. self.log.debug('B-Video-Position calculated to %u/%u',
  182. apos[0], apos[1])
  183. try:
  184. bsize = [int(i) for i in Config.get('side-by-side-preview',
  185. 'bsize').split('x', 1)]
  186. self.log.debug('B-Video-Size configured to %ux%u',
  187. bsize[0], bsize[1])
  188. except:
  189. bsize = [
  190. int(width / 4), # 25%
  191. int(height / 4) # 25%
  192. ]
  193. self.log.debug('B-Video-Size calculated to %ux%u',
  194. bsize[0], bsize[1])
  195. try:
  196. bpos = [int(i) for i in Config.get('side-by-side-preview',
  197. 'bpos').split('/', 1)]
  198. self.log.debug('B-Video-Position configured to %u/%u',
  199. bpos[0], bpos[1])
  200. except:
  201. bpos = [
  202. width - int(width / 100) - bsize[0],
  203. height - int(width / 100) - bsize[1] # 1%
  204. ]
  205. self.log.debug('B-Video-Position calculated to %u/%u',
  206. bpos[0], bpos[1])
  207. for idx, name in enumerate(self.names):
  208. pad = self.padState[idx]
  209. pad.reset()
  210. if idx == self.sourceA:
  211. pad.xpos, pad.ypos = apos
  212. pad.width, pad.height = asize
  213. pad.zorder = 1
  214. elif idx == self.sourceB:
  215. pad.xpos, pad.ypos = bpos
  216. pad.width, pad.height = bsize
  217. pad.zorder = 2
  218. else:
  219. pad.alpha = 0
  220. def recalculateMixerStatePictureInPicture(self):
  221. self.log.info('Updating Mixer-State for '
  222. 'Picture-in-Picture-Composition')
  223. width, height = self.getInputVideoSize()
  224. self.log.debug('Video-Size parsed as %ux%u', width, height)
  225. try:
  226. pipsize = [int(i) for i in Config.get('picture-in-picture',
  227. 'pipsize').split('x', 1)]
  228. self.log.debug('PIP-Size configured to %ux%u',
  229. pipsize[0], pipsize[1])
  230. except:
  231. pipsize = [
  232. int(width / 4), # 25%
  233. int(height / 4) # 25%
  234. ]
  235. self.log.debug('PIP-Size calculated to %ux%u',
  236. pipsize[0], pipsize[1])
  237. try:
  238. pippos = [int(i) for i in Config.get('picture-in-picture',
  239. 'pippos').split('/', 1)]
  240. self.log.debug('PIP-Position configured to %u/%u',
  241. pippos[0], pippos[1])
  242. except:
  243. pippos = [
  244. width - pipsize[0] - int(width / 100), # 1%
  245. height - pipsize[1] - int(width / 100) # 1%
  246. ]
  247. self.log.debug('PIP-Position calculated to %u/%u',
  248. pippos[0], pippos[1])
  249. for idx, name in enumerate(self.names):
  250. pad = self.padState[idx]
  251. pad.reset()
  252. if idx == self.sourceA:
  253. pass
  254. elif idx == self.sourceB:
  255. pad.xpos, pad.ypos = pippos
  256. pad.width, pad.height = pipsize
  257. pad.zorder = 2
  258. else:
  259. pad.alpha = 0
  260. def applyMixerState(self):
  261. for idx, state in enumerate(self.padState):
  262. # mixerpad 0 = background
  263. mixerpad = (self.mixingPipeline
  264. .get_by_name('mix')
  265. .get_static_pad('sink_%u' % (idx + 1)))
  266. self.log.debug('Reconfiguring Mixerpad %u to '
  267. 'x/y=%u/%u, w/h=%u/%u alpha=%0.2f, zorder=%u',
  268. idx, state.xpos, state.ypos,
  269. state.width, state.height,
  270. state.alpha, state.zorder)
  271. mixerpad.set_property('xpos', state.xpos)
  272. mixerpad.set_property('ypos', state.ypos)
  273. mixerpad.set_property('width', state.width)
  274. mixerpad.set_property('height', state.height)
  275. mixerpad.set_property('alpha', state.alpha)
  276. mixerpad.set_property('zorder', state.zorder)
  277. def selectCompositeModeDefaultSources(self):
  278. sectionNames = {
  279. CompositeModes.fullscreen: 'fullscreen',
  280. CompositeModes.side_by_side_equal: 'side-by-side-equal',
  281. CompositeModes.side_by_side_preview: 'side-by-side-preview',
  282. CompositeModes.picture_in_picture: 'picture-in-picture'
  283. }
  284. compositeModeName = self.compositeMode.name
  285. sectionName = sectionNames[self.compositeMode]
  286. try:
  287. defSource = Config.get(sectionName, 'default-a')
  288. self.setVideoSourceA(self.names.index(defSource))
  289. self.log.info('Changing sourceA to default of Mode %s: %s',
  290. compositeModeName, defSource)
  291. except Exception as e:
  292. pass
  293. try:
  294. defSource = Config.get(sectionName, 'default-b')
  295. self.setVideoSourceB(self.names.index(defSource))
  296. self.log.info('Changing sourceB to default of Mode %s: %s',
  297. compositeModeName, defSource)
  298. except Exception as e:
  299. pass
  300. def on_handoff(self, object, buffer):
  301. if self.padStateDirty:
  302. self.padStateDirty = False
  303. self.log.debug('[Streaming-Thread]: Pad-State is Dirty, '
  304. 'applying new Mixer-State')
  305. self.applyMixerState()
  306. def on_eos(self, bus, message):
  307. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  308. def on_error(self, bus, message):
  309. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  310. (error, debug) = message.parse_error()
  311. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  312. def setVideoSourceA(self, source):
  313. # swap if required
  314. if self.sourceB == source:
  315. self.sourceB = self.sourceA
  316. self.sourceA = source
  317. self.recalculateMixerState()
  318. def getVideoSourceA(self):
  319. return self.sourceA
  320. def setVideoSourceB(self, source):
  321. # swap if required
  322. if self.sourceA == source:
  323. self.sourceA = self.sourceB
  324. self.sourceB = source
  325. self.recalculateMixerState()
  326. def getVideoSourceB(self):
  327. return self.sourceB
  328. def setCompositeMode(self, mode):
  329. self.compositeMode = mode
  330. self.selectCompositeModeDefaultSources()
  331. self.recalculateMixerState()
  332. def getCompositeMode(self):
  333. return self.compositeMode