summaryrefslogtreecommitdiff
path: root/voctocore/lib/shmsrc.py
blob: 1158887edc78170ce4887a7b95e8fcdb47d671e2 (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, failsrc):
  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.capsfilter = Gst.ElementFactory.make('capsfilter', None)
  17. self.failsrcsyncer = Gst.ElementFactory.make('identity', None)
  18. self.switch = Gst.ElementFactory.make('input-selector', None)
  19. self.failsrc = failsrc;
  20. if not self.shmsrc or not self.capsfilter or not self.failsrcsyncer 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.capsfilter)
  25. self.add(self.failsrcsyncer)
  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.switch.set_property('active-pad', self.failpad)
  36. self.failsrcsyncer.set_property('sync', True)
  37. self.capsfilter.set_property('caps', caps)
  38. # Link elements
  39. self.shmsrc.link(self.capsfilter)
  40. self.capsfilter.get_static_pad('src').link(self.goodpad)
  41. self.failsrc.link_filtered(self.failsrcsyncer, caps)
  42. self.failsrcsyncer.get_static_pad('src').link(self.failpad)
  43. # Install pad probes
  44. self.shmsrc.get_static_pad('src').add_probe(Gst.PadProbeType.BLOCK | Gst.PadProbeType.EVENT_DOWNSTREAM, self.event_probe, None)
  45. self.shmsrc.get_static_pad('src').add_probe(Gst.PadProbeType.BLOCK | Gst.PadProbeType.BUFFER, self.data_probe, None)
  46. # Install Watchdog
  47. GLib.timeout_add(250, self.watchdog)
  48. # Add Ghost Pads
  49. self.add_pad(
  50. Gst.GhostPad.new('src', self.switch.get_static_pad('src'))
  51. )
  52. def do_handle_message(self, msg):
  53. if msg.type == Gst.MessageType.ERROR and msg.src == self.shmsrc:
  54. (err, debug) = msg.parse_error()
  55. self.log.warning('received error-message from ShmSrc, dropping: %s', err)
  56. self.log.debug(' debug-info from shmsrc: %s', debug)
  57. else:
  58. Gst.Bin.do_handle_message(self, msg)
  59. def event_probe(self, pad, info, ud):
  60. e = info.get_event()
  61. if e.type == Gst.EventType.EOS:
  62. self.log.warning('received EOS-event on event-probe, dropping')
  63. self.switch_to_failstate()
  64. return Gst.PadProbeReturn.DROP
  65. return Gst.PadProbeReturn.PASS
  66. def data_probe(self, pad, info, ud):
  67. self.last_buffer_arrived = time.time()
  68. self.switch_to_goodstate()
  69. return Gst.PadProbeReturn.PASS
  70. def watchdog(self):
  71. t = time.time()
  72. if self.last_buffer_arrived + 0.1 < t:
  73. self.log.warning('watchdog encountered a timeout')
  74. self.switch_to_failstate()
  75. if self.is_in_failstate and self.last_restart_retry + 1 < t:
  76. self.last_restart_retry = t
  77. self.restart()
  78. return True
  79. def restart(self):
  80. self.log.warning('restarting ShmSrc')
  81. self.shmsrc.set_state(Gst.State.NULL)
  82. self.shmsrc.set_base_time(self.get_parent().get_base_time())
  83. self.shmsrc.set_state(Gst.State.PLAYING)
  84. def switch_to_goodstate(self):
  85. if not self.is_in_failstate:
  86. return
  87. self.log.warning('switching output to goodstate')
  88. self.is_in_failstate = False
  89. self.switch.set_property('active-pad', self.goodpad)
  90. def switch_to_failstate(self):
  91. if self.is_in_failstate:
  92. return
  93. self.log.warning('switching output to failstate')
  94. self.is_in_failstate = True
  95. self.switch.set_property('active-pad', self.failpad)