aboutsummaryrefslogtreecommitdiff
path: root/voctogui/lib/warningoverlay.py
blob: 3cbe13cb13af7427e15452920871bc771a9be4b7 (plain)
  1. import logging
  2. from gi.repository import GLib, Gst, cairo
  3. from lib.config import Config
  4. class VideoWarningOverlay(object):
  5. """ Displays a Warning-Overlay above the Video-Feed of another VideoDisplay """
  6. def __init__(self):
  7. self.log = logging.getLogger('VideoWarningOverlay')
  8. self.text = None
  9. self.enabled = False
  10. self.blink_state = False
  11. GLib.timeout_add_seconds(1, self.on_blink_callback)
  12. caps_string = Config.get('mix', 'videocaps')
  13. self.log.debug('parsing video-caps: %s', caps_string)
  14. caps = Gst.Caps.from_string(caps_string)
  15. struct = caps.get_structure(0)
  16. _, self.width = struct.get_int('width')
  17. _, self.height = struct.get_int('height')
  18. self.log.debug('configuring size to %ux%u', self.width, self.height)
  19. def on_blink_callback(self):
  20. self.blink_state = not self.blink_state
  21. return True
  22. def enable(self, text=None):
  23. self.text = text
  24. self.enabled = True
  25. def set_text(self, text=None):
  26. self.text = text
  27. def disable(self):
  28. self.enabled = False
  29. def draw_callback(self, cr, timestamp, duration):
  30. if not self.enabled:
  31. return
  32. w = self.width
  33. h = self.height / 20
  34. # during startup, cr is sometimes another kind of context,
  35. # which does not expose set_source_rgba and other methods.
  36. # this check avoids the exceptions that would be thrown then.
  37. if isinstance(cr, cairo.Context):
  38. return
  39. if self.blink_state:
  40. cr.set_source_rgba(1.0, 0.0, 0.0, 0.8)
  41. else:
  42. cr.set_source_rgba(1.0, 0.5, 0.0, 0.8)
  43. cr.rectangle(0, 0, w, h)
  44. cr.fill()
  45. text = "Stream is Blanked"
  46. if self.text:
  47. text += ": "+self.text
  48. cr.set_font_size(h*0.75)
  49. xbearing, ybearing, txtwidth, txtheight, xadvance, yadvance = cr.text_extents(text)
  50. cr.move_to(w/2 - txtwidth/2, h*0.75)
  51. cr.set_source_rgba(1.0, 1.0, 1.0, 1.0)
  52. cr.show_text(text)