aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/streamblanker.py
blob: fea3d6a40b0a80ac44c16c0a9bb8991344250fbc (plain)
  1. import logging
  2. from gi.repository import Gst
  3. from enum import Enum
  4. from lib.config import Config
  5. from lib.clock import Clock
  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.mixingPipeline.use_clock(Clock)
  45. self.log.debug('Binding Error & End-of-Stream-Signal on Mixing-Pipeline')
  46. self.mixingPipeline.bus.add_signal_watch()
  47. self.mixingPipeline.bus.connect("message::eos", self.on_eos)
  48. self.mixingPipeline.bus.connect("message::error", self.on_error)
  49. self.log.debug('Initializing Mixer-State')
  50. self.blankSource = None
  51. self.applyMixerState()
  52. self.log.debug('Launching Mixing-Pipeline')
  53. self.mixingPipeline.set_state(Gst.State.PLAYING)
  54. def on_eos(self, bus, message):
  55. self.log.debug('Received End-of-Stream-Signal on Mixing-Pipeline')
  56. def on_error(self, bus, message):
  57. self.log.debug('Received Error-Signal on Mixing-Pipeline')
  58. (error, debug) = message.parse_error()
  59. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  60. def applyMixerState(self):
  61. self.applyMixerStateAudio()
  62. self.applyMixerStateVideo()
  63. def applyMixerStateAudio(self):
  64. mixpad = self.mixingPipeline.get_by_name('amix').get_static_pad('sink_0')
  65. blankpad = self.mixingPipeline.get_by_name('amix').get_static_pad('sink_1')
  66. mixpad.set_property('volume', int(self.blankSource is None))
  67. blankpad.set_property('volume', int(self.blankSource is not None))
  68. def applyMixerStateVideo(self):
  69. mixpad = self.mixingPipeline.get_by_name('vmix').get_static_pad('sink_0')
  70. mixpad.set_property('alpha', int(self.blankSource is None))
  71. for idx, name in enumerate(self.names):
  72. blankpad = self.mixingPipeline.get_by_name('vmix').get_static_pad('sink_%u' % (idx+1))
  73. blankpad.set_property('alpha', int(self.blankSource == idx))
  74. def setBlankSource(self, source):
  75. self.blankSource = source
  76. self.applyMixerState()