aboutsummaryrefslogtreecommitdiff
path: root/voctogui/lib/videopreviews.py
blob: b8c99ac4bf2c5a3151b1a3ea99d073e54d2c8b2c (plain)
  1. import logging
  2. from gi.repository import Gst, Gtk
  3. from lib.config import Config
  4. from lib.videodisplay import VideoDisplay
  5. import lib.connection as Connection
  6. class VideoPreviewsController(object):
  7. """Displays Video-Previews and selection Buttons for them"""
  8. def __init__(self, drawing_area, win, uibuilder):
  9. self.log = logging.getLogger('VideoPreviewsController')
  10. self.drawing_area = drawing_area
  11. self.win = win
  12. self.sources = Config.getlist('mix', 'sources')
  13. self.preview_players = {}
  14. self.previews = {}
  15. self.a_btns = {}
  16. self.b_btns = {}
  17. self.current_source = {'a': None, 'b': None}
  18. try:
  19. width = Config.getint('previews', 'width')
  20. self.log.debug('Preview-Width configured to %u', width)
  21. except:
  22. width = 320
  23. self.log.debug('Preview-Width selected as %u', width)
  24. try:
  25. height = Config.getint('previews', 'height')
  26. self.log.debug('Preview-Height configured to %u', height)
  27. except:
  28. height = width * 9 / 16
  29. self.log.debug('Preview-Height calculated to %u', height)
  30. # Accelerators
  31. accelerators = Gtk.AccelGroup()
  32. win.add_accel_group(accelerators)
  33. group_a = None
  34. group_b = None
  35. for idx, source in enumerate(self.sources):
  36. self.log.info('Initializing Video Preview %s', source)
  37. preview = uibuilder.get_check_widget('widget_preview', clone=True)
  38. video = uibuilder.find_widget_recursive(preview, 'video')
  39. video.set_size_request(width, height)
  40. drawing_area.pack_start(preview, fill=False,
  41. expand=False, padding=0)
  42. player = VideoDisplay(video, port=13000 + idx,
  43. width=width, height=height)
  44. uibuilder.find_widget_recursive(preview, 'label').set_label(source)
  45. btn_a = uibuilder.find_widget_recursive(preview, 'btn_a')
  46. btn_b = uibuilder.find_widget_recursive(preview, 'btn_b')
  47. btn_a.set_name("%c %u" % ('a', idx))
  48. btn_b.set_name("%c %u" % ('b', idx))
  49. if not group_a:
  50. group_a = btn_a
  51. else:
  52. btn_a.join_group(group_a)
  53. if not group_b:
  54. group_b = btn_b
  55. else:
  56. btn_b.join_group(group_b)
  57. btn_a.connect('toggled', self.btn_toggled)
  58. btn_b.connect('toggled', self.btn_toggled)
  59. key, mod = Gtk.accelerator_parse('%u' % (idx + 1))
  60. btn_a.add_accelerator('activate', accelerators,
  61. key, mod, Gtk.AccelFlags.VISIBLE)
  62. tooltip = Gtk.accelerator_get_label(key, mod)
  63. btn_a.set_tooltip_text(tooltip)
  64. key, mod = Gtk.accelerator_parse('<Ctrl>%u' % (idx + 1))
  65. btn_b.add_accelerator('activate', accelerators,
  66. key, mod, Gtk.AccelFlags.VISIBLE)
  67. tooltip = Gtk.accelerator_get_label(key, mod)
  68. btn_b.set_tooltip_text(tooltip)
  69. btn_fullscreen = uibuilder.find_widget_recursive(preview,
  70. 'btn_fullscreen')
  71. btn_fullscreen.set_name("%c %u" % ('f', idx))
  72. btn_fullscreen.connect('clicked', self.btn_fullscreen_clicked)
  73. key, mod = Gtk.accelerator_parse('<Alt>%u' % (idx + 1))
  74. btn_fullscreen.add_accelerator('activate', accelerators,
  75. key, mod, Gtk.AccelFlags.VISIBLE)
  76. tooltip = Gtk.accelerator_get_label(key, mod)
  77. btn_fullscreen.set_tooltip_text(tooltip)
  78. self.preview_players[source] = player
  79. self.previews[source] = preview
  80. self.a_btns[source] = btn_a
  81. self.b_btns[source] = btn_b
  82. # connect event-handler and request initial state
  83. Connection.on('video_status', self.on_video_status)
  84. Connection.send('get_video')
  85. def btn_toggled(self, btn):
  86. if not btn.get_active():
  87. return
  88. btn_name = btn.get_name()
  89. self.log.debug('btn_toggled: %s', btn_name)
  90. channel, idx = btn_name.split(' ')[:2]
  91. source_name = self.sources[int(idx)]
  92. if self.current_source[channel] == source_name:
  93. self.log.info('video-channel %s already on %s',
  94. channel, source_name)
  95. return
  96. self.log.info('video-channel %s changed to %s', channel, source_name)
  97. Connection.send('set_video_' + channel, source_name)
  98. def btn_fullscreen_clicked(self, btn):
  99. btn_name = btn.get_name()
  100. self.log.debug('btn_fullscreen_clicked: %s', btn_name)
  101. channel, idx = btn_name.split(' ')[:2]
  102. source_name = self.sources[int(idx)]
  103. self.log.info('selcting video %s for fullscreen', source_name)
  104. Connection.send('set_videos_and_composite',
  105. source_name, '*', 'fullscreen')
  106. def on_video_status(self, source_a, source_b):
  107. self.log.info('on_video_status callback w/ sources: %s and %s',
  108. source_a, source_b)
  109. self.current_source['a'] = source_a
  110. self.current_source['b'] = source_b
  111. self.a_btns[source_a].set_active(True)
  112. self.b_btns[source_b].set_active(True)