summaryrefslogtreecommitdiff
path: root/voctocore/lib/shmsrc.py
blob: 76b95e8fd37a97ac57f2fcf50f3b62f0d2fcbb4e (plain)
  1. #!/usr/bin/python3
  2. import time, logging
  3. from gi.repository import GLib, Gst
  4. from lib.config import Config
  5. class FailsafeShmSrc(Gst.Bin):
  6. log = logging.getLogger('FailsafeShmSrc')
  7. last_buffer_arrived = 0
  8. last_restart_retry = 0
  9. is_in_failstate = True
  10. def __init__(self, socket):
  11. super().__init__()
  12. caps = Gst.Caps.from_string(Config.get('sources', 'videocaps'))
  13. self.log.debug('parsing videocaps from config: %s', caps.to_string())
  14. # Create elements
  15. self.shmsrc = Gst.ElementFactory.make('shmsrc', None)
  16. self.identity1 = Gst.ElementFactory.make('identity', None)
  17. self.identity2 = Gst.ElementFactory.make('identity', None)
  18. self.switch = Gst.ElementFactory.make('input-selector', None)
  19. self.failsrc = Gst.ElementFactory.make('videotestsrc', None)
  20. if not self.shmsrc or not self.identity1 or not self.identity2 or not self.switch or not self.failsrc:
  21. self.log.error('could not create elements')
  22. # Add elements to Bin
  23. self.add(self.shmsrc)
  24. self.add(self.identity1)
  25. self.add(self.identity2)
  26. self.add(self.switch)
  27. self.add(self.failsrc)
  28. # Get Switcher-Pads
  29. self.goodpad = self.switch.get_request_pad('sink_%u')
  30. self.failpad = self.switch.get_request_pad('sink_%u')
  31. # Set properties
  32. self.shmsrc.set_property('socket-path', socket)
  33. self.shmsrc.set_property('is-live', True)
  34. self.shmsrc.set_property('do-timestamp', True)
  35. self.identity2.set_property('sync', True)
  36. self.switch.set_property('active-pad', self.failpad)
  37. # Link elements
  38. self.shmsrc.link_filtered(self.identity1, caps)
  39. self.identity1.get_static_pad('src').link(self.goodpad)
  40. self.failsrc.link_filtered(self.identity2, caps)
  41. self.identity2.get_static_pad('src').link(self.failpad)
  42. # Install pad probes
  43. self.shmsrc.get_static_pad('src').add_probe(Gst.PadProbeType.BLOCK | Gst.PadProbeType.EVENT_DOWNSTREAM, self.event_probe, None)
  44. self.shmsrc.get_static_pad('src').add_probe(Gst.PadProbeType.BLOCK | Gst.PadProbeType.BUFFER, self.data_probe, None)
  45. # Install Watchdog
  46. GLib.timeout_add(250, self.watchdog)
  47. # Add Ghost Pads
  48. self.add_pad(
  49. Gst.GhostPad.new('src', self.switch.get_static_pad('src'))
  50. )
  51. def do_handle_message(self, msg):
  52. if msg.type == Gst.MessageType.ERROR and msg.src == self.shmsrc:
  53. (err, debug) = msg.parse_error()
  54. self.log.warning('received error-message from ShmSrc, dropping: %s', err)
  55. self.log.debug(' debug-info from shmsrc: %s', debug)
  56. else:
  57. Gst.Bin.do_handle_message(self, msg)
  58. def event_probe(self, pad, info, ud):
  59. e = info.get_event()
  60. if e.type == Gst.EventType.EOS:
  61. self.log.warning('received EOS-event on event-probe, dropping')
  62. self.switch_to_failstate()
  63. return Gst.PadProbeReturn.DROP
  64. return Gst.PadProbeReturn.PASS
  65. def data_probe(self, pad, info, ud):
  66. self.last_buffer_arrived = time.time()
  67. self.switch_to_goodstate()
  68. return Gst.PadProbeReturn.PASS
  69. def watchdog(self):
  70. t = time.time()
  71. if self.last_buffer_arrived + 0.1 < t:
  72. self.log.warning('watchdog encountered a timeout')
  73. self.switch_to_failstate()
  74. if self.is_in_failstate and self.last_restart_retry + 1 < t:
  75. self.last_restart_retry = t
  76. self.restart()
  77. return True
  78. def restart(self):
  79. self.log.warning('restarting ShmSrc')
  80. self.shmsrc.set_state(Gst.State.NULL)
  81. self.shmsrc.set_base_time(self.get_parent().get_base_time())
  82. self.shmsrc.set_state(Gst.State.PLAYING)
  83. def switch_to_goodstate(self):
  84. if not self.is_in_failstate:
  85. return
  86. self.log.warning('switching output to goodstate')
  87. self.is_in_failstate = False
  88. self.switch.set_property('active-pad', self.goodpad)
  89. def switch_to_failstate(self):
  90. if self.is_in_failstate:
  91. return
  92. self.log.warning('switching output to failstate')
  93. self.is_in_failstate = True
  94. self.switch.set_property('active-pad', self.failpad)