summaryrefslogtreecommitdiff
path: root/voctocore/experiments/shmsrc.py
blob: 63f428d814d592b573c05e7c2899c85809953b7e (plain)
  1. #!/usr/bin/python3
  2. import time
  3. from gi.repository import GLib, Gst
  4. class ShmSrc(Gst.Bin):
  5. last_buffer_arrived = 0
  6. is_in_failstate = True
  7. def __init__(self, socket, caps):
  8. super().__init__()
  9. # Create elements
  10. self.shmsrc = Gst.ElementFactory.make('shmsrc', None)
  11. self.identity1 = Gst.ElementFactory.make('identity', None)
  12. self.identity2 = Gst.ElementFactory.make('identity', None)
  13. self.switch = Gst.ElementFactory.make('input-selector', None)
  14. self.failsrc = Gst.ElementFactory.make('videotestsrc', None)
  15. # Add elements to Bin
  16. self.add(self.shmsrc)
  17. self.add(self.identity1)
  18. self.add(self.identity2)
  19. self.add(self.switch)
  20. self.add(self.failsrc)
  21. # Get Switcher-Pads
  22. self.goodpad = self.switch.get_request_pad('sink_%u')
  23. self.failpad = self.switch.get_request_pad('sink_%u')
  24. # Set properties
  25. self.shmsrc.set_property('socket-path', socket)
  26. self.shmsrc.set_property('is-live', True)
  27. self.shmsrc.set_property('do-timestamp', True)
  28. #self.identity1.set_property('sync', True)
  29. self.identity2.set_property('sync', True)
  30. self.switch.set_property('active-pad', self.failpad)
  31. self.failsrc.set_property('pattern', 'snow')
  32. # Link elements
  33. self.shmsrc.link_filtered(self.identity1, caps)
  34. self.identity1.get_static_pad('src').link(self.goodpad)
  35. self.failsrc.link_filtered(self.identity2, caps)
  36. self.identity2.get_static_pad('src').link(self.failpad)
  37. # Install pad probes
  38. self.shmsrc.get_static_pad('src').add_probe(Gst.PadProbeType.BLOCK | Gst.PadProbeType.EVENT_DOWNSTREAM, self.event_probe, None)
  39. self.shmsrc.get_static_pad('src').add_probe(Gst.PadProbeType.BLOCK | Gst.PadProbeType.BUFFER, self.data_probe, None)
  40. # Install Watchdog
  41. GLib.timeout_add(500, self.watchdog)
  42. # Add Ghost Pads
  43. self.add_pad(
  44. Gst.GhostPad.new('sink', self.switch.get_static_pad('src'))
  45. )
  46. def event_probe(self, pad, info, ud):
  47. e = info.get_event()
  48. if e.type == Gst.EventType.EOS:
  49. self.switch_to_failstate()
  50. return Gst.PadProbeReturn.DROP
  51. return Gst.PadProbeReturn.PASS
  52. def data_probe(self, pad, info, ud):
  53. self.last_buffer_arrived = time.time()
  54. self.switch_to_goodstate()
  55. return Gst.PadProbeReturn.PASS
  56. def watchdog(self):
  57. if self.last_buffer_arrived + 0.1 < time.time():
  58. print("watchdog()::timeout")
  59. self.switch_to_failstate()
  60. if self.last_buffer_arrived + 3 < time.time() and round(time.time() % 3) == 0:
  61. print("watchdog()::restart")
  62. self.restart()
  63. return True
  64. def restart(self):
  65. self.shmsrc.set_state(Gst.State.NULL)
  66. self.shmsrc.set_state(Gst.State.PLAYING)
  67. def switch_to_goodstate(self):
  68. if not self.is_in_failstate:
  69. return
  70. print("switch_to_goodstate()")
  71. self.is_in_failstate = False
  72. self.switch.set_property('active-pad', self.goodpad)
  73. def switch_to_failstate(self):
  74. if self.is_in_failstate:
  75. return
  76. print("switch_to_failstate()")
  77. self.is_in_failstate = True
  78. self.switch.set_property('active-pad', self.failpad)