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