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