summaryrefslogtreecommitdiff
path: root/voctogui/lib/videodisplay.py
blob: 18ea3bbfb43e286c3fac7bb74adce4a0352723e7 (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} !
  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 alsa
  75. if play_audio:
  76. # ts-offset=1000000000 (1s) - should keep audio & video in sync but delay by 1s
  77. pipeline += """
  78. alsasink sync=False
  79. """
  80. # Otherwise just trash the Audio
  81. else:
  82. pipeline += """
  83. fakesink
  84. """
  85. pipeline = pipeline.format(
  86. acaps=Config.get('mix', 'audiocaps'),
  87. vcaps=Config.get('mix', 'videocaps'),
  88. previewcaps=Config.get('previews', 'videocaps'),
  89. host=Config.get('server', 'host'),
  90. port=port,
  91. )
  92. self.log.debug('Creating Display-Pipeline:\n%s', pipeline)
  93. self.pipeline = Gst.parse_launch(pipeline)
  94. self.pipeline.use_clock(Clock)
  95. self.drawing_area.realize()
  96. self.xid = self.drawing_area.get_property('window').get_xid()
  97. self.log.debug('Realized Drawing-Area with xid %u', self.xid)
  98. bus = self.pipeline.get_bus()
  99. bus.add_signal_watch()
  100. bus.enable_sync_message_emission()
  101. bus.connect('message::error', self.on_error)
  102. bus.connect("sync-message::element", self.on_syncmsg)
  103. if self.level_callback:
  104. bus.connect("message::element", self.on_level)
  105. self.log.debug('Launching Display-Pipeline')
  106. self.pipeline.set_state(Gst.State.PLAYING)
  107. def on_syncmsg(self, bus, msg):
  108. if msg.get_structure().get_name() == "prepare-window-handle":
  109. self.log.info('Setting imagesink window-handle to %s', self.xid)
  110. msg.src.set_window_handle(self.xid)
  111. def on_error(self, bus, message):
  112. self.log.debug('Received Error-Signal on Display-Pipeline')
  113. (error, debug) = message.parse_error()
  114. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  115. def on_level(self, bus, msg):
  116. if msg.src.name != 'lvl':
  117. return
  118. if msg.type != Gst.MessageType.ELEMENT:
  119. return
  120. rms = msg.get_structure().get_value('rms')
  121. peak = msg.get_structure().get_value('peak')
  122. decay = msg.get_structure().get_value('decay')
  123. self.level_callback(rms, peak, decay)