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