aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/streamblanker.py
blob: 2f4cc58b781c773e514ebf65ade6f4b5965669ba (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 StreamBlanker(object):
  7. log = logging.getLogger('StreamBlanker')
  8. def __init__(self):
  9. self.acaps = Config.get('mix', 'audiocaps')
  10. self.vcaps = Config.get('mix', 'videocaps')
  11. self.names = Config.getlist('stream-blanker', 'sources')
  12. self.log.info('Configuring StreamBlanker video %u Sources', len(self.names))
  13. pipeline = """
  14. compositor name=vmix !
  15. {vcaps} !
  16. intervideosink channel=video_streamblanker_out
  17. audiomixer name=amix !
  18. {acaps} !
  19. interaudiosink channel=audio_streamblanker_out
  20. intervideosrc channel=video_mix_streamblanker !
  21. {vcaps} !
  22. vmix.
  23. interaudiosrc channel=audio_mix_streamblanker !
  24. {acaps} !
  25. amix.
  26. interaudiosrc channel=audio_streamblanker !
  27. {acaps} !
  28. amix.
  29. """.format(
  30. acaps=self.acaps,
  31. vcaps=self.vcaps
  32. )
  33. for name in self.names:
  34. pipeline += """
  35. intervideosrc channel=video_{name}_streamblanker !
  36. {vcaps} !
  37. vmix.
  38. """.format(
  39. name=name,
  40. vcaps=self.vcaps
  41. )
  42. self.log.debug('Creating Mixing-Pipeline:\n%s', pipeline)
  43. self.mixingPipeline = Gst.parse_launch(pipeline)
  44. self.log.debug('Binding Error & End-of-Stream-Signal on Mixing-Pipeline')
  45. self.mixingPipeline.bus.add_signal_watch()
  46. self.mixingPipeline.bus.connect("message::eos", self.on_eos)
  47. self.mixingPipeline.bus.connect("message::error", self.on_error)
  48. self.log.debug('Initializing Mixer-State')
  49. self.blankSource = None
  50. self.applyMixerState()
  51. self.log.debug('Launching Mixing-Pipeline')
  52. self.mixingPipeline.set_state(Gst.State.PLAYING)
  53. def on_eos(self, bus, message):
  54. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  55. def on_error(self, bus, message):
  56. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  57. (error, debug) = message.parse_error()
  58. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  59. def applyMixerState(self):
  60. self.applyMixerStateAudio()
  61. self.applyMixerStateVideo()
  62. def applyMixerStateAudio(self):
  63. mixpad = self.mixingPipeline.get_by_name('amix').get_static_pad('sink_0')
  64. blankpad = self.mixingPipeline.get_by_name('amix').get_static_pad('sink_1')
  65. mixpad.set_property('volume', int(self.blankSource is None))
  66. blankpad.set_property('volume', int(self.blankSource is not None))
  67. def applyMixerStateVideo(self):
  68. mixpad = self.mixingPipeline.get_by_name('vmix').get_static_pad('sink_0')
  69. mixpad.set_property('alpha', int(self.blankSource is None))
  70. for idx, name in enumerate(self.names):
  71. blankpad = self.mixingPipeline.get_by_name('vmix').get_static_pad('sink_%u' % (idx+1))
  72. blankpad.set_property('alpha', int(self.blankSource == idx))
  73. def setBlankSource(self, source):
  74. self.blankSource = source
  75. self.applyMixerState()