aboutsummaryrefslogtreecommitdiff
path: root/voctogui/lib/videodisplay.py
blob: 8ce1413157025ab3070cf6333a7cd1fac0241c45 (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, width=None, height=None, play_audio=False, level_callback=None):
  7. self.log = logging.getLogger('VideoDisplay[%u]' % port)
  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. videosystem = Config.get('videodisplay', 'system')
  40. self.log.debug('Configuring for Video-System %s', videosystem)
  41. if videosystem == 'gl':
  42. pipeline += """
  43. glupload !
  44. glcolorconvert !
  45. glimagesinkelement
  46. """
  47. elif videosystem == 'xv':
  48. pipeline += """
  49. xvimagesink
  50. """
  51. elif videosystem == 'x':
  52. prescale_caps = 'video/x-raw'
  53. if width and height:
  54. prescale_caps += ',width=%u,height=%u' % (width, height)
  55. pipeline += """
  56. videoconvert !
  57. videoscale !
  58. {prescale_caps} !
  59. ximagesink
  60. """.format(
  61. prescale_caps=prescale_caps
  62. )
  63. else:
  64. raise Exception('Invalid Videodisplay-System configured: %s'. system)
  65. # If an Audio-Path is required, add an Audio-Path through a level-Element
  66. if self.level_callback or play_audio:
  67. pipeline += """
  68. demux. !
  69. {acaps} !
  70. queue !
  71. level name=lvl interval=50000000 !
  72. """
  73. # If Playback is requested, push fo alsa
  74. if play_audio:
  75. # ts-offset=1000000000 (1s) - should keep audio & video in sync but delay by 1s
  76. pipeline += """
  77. alsasink sync=False
  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.drawing_area.realize()
  94. self.xid = self.drawing_area.get_property('window').get_xid()
  95. self.log.debug('Realized Drawing-Area with xid %u', self.xid)
  96. bus = self.pipeline.get_bus()
  97. bus.add_signal_watch()
  98. bus.enable_sync_message_emission()
  99. bus.connect('message::error', self.on_error)
  100. bus.connect("sync-message::element", self.on_syncmsg)
  101. if self.level_callback:
  102. bus.connect("message::element", self.on_level)
  103. self.log.debug('Launching Display-Pipeline')
  104. self.pipeline.set_state(Gst.State.PLAYING)
  105. def on_syncmsg(self, bus, msg):
  106. if msg.get_structure().get_name() == "prepare-window-handle":
  107. self.log.info('Setting imagesink window-handle to %s', self.xid)
  108. msg.src.set_window_handle(self.xid)
  109. def on_error(self, bus, message):
  110. self.log.debug('Received Error-Signal on Display-Pipeline')
  111. (error, debug) = message.parse_error()
  112. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  113. def on_level(self, bus, msg):
  114. if msg.src.name != 'lvl':
  115. return
  116. if msg.type != Gst.MessageType.ELEMENT:
  117. return
  118. rms = msg.get_structure().get_value('rms')
  119. peak = msg.get_structure().get_value('peak')
  120. decay = msg.get_structure().get_value('decay')
  121. self.level_callback(rms, peak, decay)