aboutsummaryrefslogtreecommitdiff
path: root/voctogui/lib/videodisplay.py
blob: 78dafba3ca750d3f61c9562f182a040163ffc943 (plain)
  1. import logging
  2. from gi.repository import Gst
  3. from lib.config import Config
  4. from lib.clock import Clock
  5. class VideoDisplay(object):
  6. """ Displays a Voctomix-Video-Stream into a GtkWidget """
  7. def __init__(self, drawing_area, port, width=None, height=None, play_audio=False, level_callback=None):
  8. self.log = logging.getLogger('VideoDisplay[%u]' % port)
  9. self.drawing_area = drawing_area
  10. self.level_callback = level_callback
  11. caps = Config.get('mix', 'videocaps')
  12. use_previews = Config.getboolean('previews', 'enabled') and Config.getboolean('previews', 'use')
  13. # Preview-Ports are Raw-Ports + 1000
  14. if use_previews:
  15. self.log.info('using jpeg-previews instead of raw-video for gui')
  16. port += 1000
  17. else:
  18. self.log.info('using raw-video instead of jpeg-previews for gui')
  19. # Setup Server-Connection, Demuxing and Decoding
  20. pipeline = """
  21. tcpclientsrc host={host} port={port} blocksize=1048576 !
  22. queue !
  23. matroskademux name=demux
  24. """
  25. if use_previews:
  26. pipeline += """
  27. demux. !
  28. image/jpeg !
  29. jpegdec !
  30. {previewcaps} !
  31. queue !
  32. """
  33. else:
  34. pipeline += """
  35. demux. !
  36. {vcaps} !
  37. queue !
  38. """
  39. # Video Display
  40. videosystem = Config.get('videodisplay', 'system')
  41. self.log.debug('Configuring for Video-System %s', videosystem)
  42. if videosystem == 'gl':
  43. pipeline += """
  44. glupload !
  45. glcolorconvert !
  46. glimagesinkelement
  47. """
  48. elif videosystem == 'xv':
  49. pipeline += """
  50. xvimagesink
  51. """
  52. elif videosystem == 'x':
  53. prescale_caps = 'video/x-raw'
  54. if width and height:
  55. prescale_caps += ',width=%u,height=%u' % (width, height)
  56. pipeline += """
  57. videoconvert !
  58. videoscale !
  59. {prescale_caps} !
  60. ximagesink
  61. """.format(
  62. prescale_caps=prescale_caps
  63. )
  64. else:
  65. raise Exception('Invalid Videodisplay-System configured: %s'. system)
  66. # If an Audio-Path is required, add an Audio-Path through a level-Element
  67. if self.level_callback or play_audio:
  68. pipeline += """
  69. demux. !
  70. {acaps} !
  71. queue !
  72. level name=lvl interval=50000000 !
  73. """
  74. # If Playback is requested, push fo pulseaudio
  75. if play_audio:
  76. pipeline += """
  77. pulsesink
  78. """
  79. # Otherwise just trash the Audio
  80. else:
  81. pipeline += """
  82. fakesink
  83. """
  84. pipeline = pipeline.format(
  85. acaps=Config.get('mix', 'audiocaps'),
  86. vcaps=Config.get('mix', 'videocaps'),
  87. previewcaps=Config.get('previews', 'videocaps'),
  88. host=Config.get('server', 'host'),
  89. port=port,
  90. )
  91. self.log.debug('Creating Display-Pipeline:\n%s', pipeline)
  92. self.pipeline = Gst.parse_launch(pipeline)
  93. self.pipeline.use_clock(Clock)
  94. self.drawing_area.realize()
  95. self.xid = self.drawing_area.get_property('window').get_xid()
  96. self.log.debug('Realized Drawing-Area with xid %u', self.xid)
  97. bus = self.pipeline.get_bus()
  98. bus.add_signal_watch()
  99. bus.enable_sync_message_emission()
  100. bus.connect('message::error', self.on_error)
  101. bus.connect("sync-message::element", self.on_syncmsg)
  102. if self.level_callback:
  103. bus.connect("message::element", self.on_level)
  104. self.log.debug('Launching Display-Pipeline')
  105. self.pipeline.set_state(Gst.State.PLAYING)
  106. def on_syncmsg(self, bus, msg):
  107. if msg.get_structure().get_name() == "prepare-window-handle":
  108. self.log.info('Setting imagesink window-handle to %s', self.xid)
  109. msg.src.set_window_handle(self.xid)
  110. def on_error(self, bus, message):
  111. self.log.debug('Received Error-Signal on Display-Pipeline')
  112. (error, debug) = message.parse_error()
  113. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  114. def on_level(self, bus, msg):
  115. if msg.src.name != 'lvl':
  116. return
  117. if msg.type != Gst.MessageType.ELEMENT:
  118. return
  119. rms = msg.get_structure().get_value('rms')
  120. peak = msg.get_structure().get_value('peak')
  121. decay = msg.get_structure().get_value('decay')
  122. self.level_callback(rms, peak, decay)