summaryrefslogtreecommitdiff
path: root/voctocore/lib/video/mix.py
blob: a9e1513e2ba26b9b252a18edec1f328969ea4f3c (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 ComposteModes(Enum):
  7. fullscreen = 0
  8. class VideoMix(object):
  9. log = logging.getLogger('VideoMix')
  10. mixingPipeline = None
  11. caps = None
  12. names = []
  13. compositeMode = ComposteModes.fullscreen
  14. sourceA = 0
  15. sourceB = 1
  16. def __init__(self):
  17. self.caps = Config.get('mix', 'videocaps')
  18. self.names = Config.getlist('sources', 'video')
  19. self.log.info('Configuring Mixer for %u Sources', len(self.names))
  20. pipeline = """
  21. videomixer name=mix !
  22. {caps} !
  23. textoverlay text=mixer halignment=left valignment=top ypad=175 !
  24. intervideosink channel=video_mix
  25. """.format(
  26. caps=self.caps
  27. )
  28. for idx, name in enumerate(self.names):
  29. pipeline += """
  30. intervideosrc channel=video_{name}_mixer !
  31. {caps} !
  32. videoscale !
  33. capsfilter name=caps_{idx} !
  34. mix.
  35. """.format(
  36. name=name,
  37. caps=self.caps,
  38. idx=idx
  39. )
  40. self.log.debug('Creating Mixing-Pipeline:\n%s', pipeline)
  41. self.mixingPipeline = Gst.parse_launch(pipeline)
  42. self.log.debug('Initializing Mixer-State')
  43. self.updateMixerState()
  44. self.log.debug('Launching Mixing-Pipeline:\n%s', pipeline)
  45. self.mixingPipeline.set_state(Gst.State.PLAYING)
  46. def updateMixerState(self):
  47. if self.compositeMode == ComposteModes.fullscreen:
  48. self.updateMixerStateFullscreen()
  49. def updateMixerStateFullscreen(self):
  50. self.log.info('Updating Mixer-State for Fullscreen-Composition')
  51. for idx, name in enumerate(self.names):
  52. alpha = int(idx == self.sourceA)
  53. self.log.debug('Setting Mixerpad %u to x/y=0 and alpha=%u', idx, alpha)
  54. mixerpad = self.mixingPipeline.get_by_name('mix').get_static_pad('sink_%u' % idx)
  55. mixerpad.set_property('alpha', alpha )
  56. mixerpad.set_property('xpos', 0)
  57. mixerpad.set_property('ypos', 0)
  58. self.log.debug('Resetting Scaler %u to non-scaling', idx)
  59. capsfilter = self.mixingPipeline.get_by_name('caps_%u' % idx)
  60. capsfilter.set_property('caps', Gst.Caps.from_string(self.caps))
  61. def setVideoA(self, source):
  62. self.sourceA = source
  63. self.updateMixerState()
  64. def setVideoB(self, source):
  65. self.sourceN = source
  66. self.updateMixerState()