summaryrefslogtreecommitdiff
path: root/voctocore/experiments/failovertest.py
blob: 5ebaa9a91d7f33dc400fa5f407fa7e8076c3c7d5 (plain)
  1. #!/usr/bin/python3
  2. import gi
  3. import time
  4. import signal
  5. from http.client import HTTPConnection
  6. from termcolor import colored
  7. from threading import Timer, Thread
  8. # import GStreamer and GTK-Helper classes
  9. gi.require_version('Gst', '1.0')
  10. from gi.repository import GLib, Gst, Gtk, GObject
  11. # init GObject before importing local classes
  12. GObject.threads_init()
  13. Gst.init(None)
  14. loop = GLib.MainLoop()
  15. # make killable by ctrl-c
  16. signal.signal(signal.SIGINT, signal.SIG_DFL)
  17. # parse_launch
  18. p = Gst.parse_launch("""
  19. input-selector name=failover ! autovideosink
  20. appsrc name=src blocksize=4096 is-live=true block=true ! multipartdemux name=demux ! jpegparse ! jpegdec ! videoconvert ! failover.sink_1
  21. videotestsrc ! video/x-raw,width=500,height=375 ! failover.sink_0
  22. """)
  23. def failsafeVideoSource():
  24. src = p.get_by_name('src')
  25. dec = p.get_by_name('dec')
  26. demux = p.get_by_name('demux')
  27. failover = p.get_by_name('failover')
  28. srcActive = True
  29. while True:
  30. print('connecting to framegrabber')
  31. try:
  32. con = HTTPConnection('beachcam.kdhnc.com', 80, timeout=3)
  33. req = con.request('GET', '/mjpg/video.mjpg?camera=1')
  34. res = con.getresponse()
  35. srcActive = True
  36. print('connected, switching to video')
  37. failover.set_property('active-pad', failover.get_static_pad('sink_1'))
  38. while srcActive:
  39. chunk = res.read(4094)
  40. chunklen = len(chunk)
  41. print('read ', len(chunk), ' of ', 4096, ', closed=', res.isclosed())
  42. if chunklen > 0:
  43. src.emit('push-buffer', Gst.Buffer.new_wrapped(chunk))
  44. else:
  45. srcActive = False
  46. except:
  47. print('exception')
  48. srcActive = False
  49. print('switching to failsave')
  50. failover.set_property('active-pad', failover.get_static_pad('sink_0'))
  51. print('sleeping before retry')
  52. time.sleep(1)
  53. fsThread = Thread(target=failsafeVideoSource)
  54. fsThread.deamon = True
  55. fsThread.start()
  56. # set playing
  57. p.set_state(Gst.State.PLAYING)
  58. # start mainloop
  59. loop.run()