summaryrefslogtreecommitdiff
path: root/voctogui/lib/videodisplay.py
blob: 4133d5ac2bd90d5a6470a9d4c7e6aeb30ed65436 (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, draw_callback=None, level_callback=None):
  7. self.log = logging.getLogger('VideoDisplay[%s]' % drawing_area.get_name())
  8. self.drawing_area = drawing_area
  9. self.draw_callback = draw_callback
  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. videoscale method=nearest-neighbour !
  32. videorate !
  33. {vcaps} !
  34. queue !
  35. """
  36. else:
  37. pipeline += """
  38. demux. !
  39. {vcaps} !
  40. queue !
  41. """
  42. # If an overlay is required, add an cairooverlay-Element into the Video-Path
  43. if self.draw_callback:
  44. pipeline += """
  45. videoconvert !
  46. cairooverlay name=overlay !
  47. videoconvert !
  48. """
  49. # Video Display
  50. pipeline += """
  51. xvimagesink name=v
  52. """
  53. # If an Audio-Path is required, add an Audio-Path through a level-Element
  54. if self.level_callback or play_audio:
  55. pipeline += """
  56. demux. !
  57. {acaps} !
  58. queue !
  59. level name=lvl interval=50000000 !
  60. """
  61. # If Playback is requested, push fo alsa
  62. if play_audio:
  63. # ts-offset=1000000000 (1s) - should keep audio & video in sync but delay by 1s
  64. pipeline += """
  65. alsasink sync=False
  66. """
  67. # Otherwise just trash the Audio
  68. else:
  69. pipeline += """
  70. fakesink
  71. """
  72. pipeline = pipeline.format(
  73. acaps=Config.get('mix', 'audiocaps'),
  74. vcaps=Config.get('mix', 'videocaps'),
  75. previewcaps=Config.get('previews', 'videocaps'),
  76. host=Config.get('server', 'host'),
  77. port=port,
  78. )
  79. self.log.debug('Creating Display-Pipeline:\n%s', pipeline)
  80. self.pipeline = Gst.parse_launch(pipeline)
  81. self.drawing_area.realize()
  82. self.xid = self.drawing_area.get_property('window').get_xid()
  83. self.log.debug('Realized Drawing-Area with xid %u', self.xid)
  84. bus = self.pipeline.get_bus()
  85. bus.add_signal_watch()
  86. bus.enable_sync_message_emission()
  87. bus.connect('message::error', self.on_error)
  88. bus.connect("sync-message::element", self.on_syncmsg)
  89. if self.level_callback:
  90. bus.connect("message::element", self.on_level)
  91. if self.draw_callback:
  92. self.pipeline.get_by_name('overlay').connect('draw', self.on_draw)
  93. self.log.debug('Launching Display-Pipeline')
  94. self.pipeline.set_state(Gst.State.PLAYING)
  95. def on_syncmsg(self, bus, msg):
  96. if msg.get_structure().get_name() == "prepare-window-handle":
  97. self.log.info('Setting xvimagesink window-handle to %s', self.xid)
  98. msg.src.set_window_handle(self.xid)
  99. def on_error(self, bus, message):
  100. self.log.debug('Received Error-Signal on Display-Pipeline')
  101. (error, debug) = message.parse_error()
  102. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  103. def on_level(self, bus, msg):
  104. if msg.src.name != 'lvl':
  105. return
  106. if msg.type != Gst.MessageType.ELEMENT:
  107. return
  108. peaks = msg.get_structure().get_value('peak')
  109. rms = msg.get_structure().get_value('rms')
  110. self.level_callback(peaks, rms)
  111. def on_draw(self, cairooverlay, cr, timestamp, duration):
  112. self.draw_callback(cr, timestamp, duration)