summaryrefslogtreecommitdiff
path: root/voctocore/lib/shmsrc.py
blob: 53a7d9de053fbb3a425d632515de13d3b2b14416 (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. is_in_failstate = True
  9. def __init__(self, name, socket):
  10. super().__init__()
  11. self.set_name(socket)
  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(500, 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. self.log.warning('received error-message from ShmSrc, dropping')
  54. else:
  55. Gst.Bin.do_handle_message(self, msg)
  56. def event_probe(self, pad, info, ud):
  57. e = info.get_event()
  58. if e.type == Gst.EventType.EOS:
  59. self.log.warning('received EOS-event on event-probe, dropping')
  60. self.switch_to_failstate()
  61. return Gst.PadProbeReturn.DROP
  62. return Gst.PadProbeReturn.PASS
  63. def data_probe(self, pad, info, ud):
  64. self.last_buffer_arrived = time.time()
  65. self.switch_to_goodstate()
  66. return Gst.PadProbeReturn.PASS
  67. def watchdog(self):
  68. if self.last_buffer_arrived + 0.1 < time.time():
  69. self.log.warning('watchdog encountered a timeout')
  70. self.switch_to_failstate()
  71. if self.last_buffer_arrived + 3 < time.time() and round(time.time() % 3) == 0:
  72. self.restart()
  73. return True
  74. def restart(self):
  75. self.log.warning('restarting ShmSrc')
  76. self.shmsrc.set_state(Gst.State.NULL)
  77. self.shmsrc.set_state(Gst.State.PLAYING)
  78. def switch_to_goodstate(self):
  79. if not self.is_in_failstate:
  80. return
  81. self.log.warning('switching output to goodstate')
  82. self.is_in_failstate = False
  83. self.switch.set_property('active-pad', self.goodpad)
  84. def switch_to_failstate(self):
  85. if self.is_in_failstate:
  86. return
  87. self.log.warning('switching output to failstate')
  88. self.is_in_failstate = True
  89. self.switch.set_property('active-pad', self.failpad)