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