aboutsummaryrefslogtreecommitdiff
path: root/example-scripts/gstreamer/source-background-loop.py
blob: be59d1857ce3bb7c67a77cab99c8c305231c8ba2 (plain)
  1. #!/usr/bin/python3
  2. import sys, gi, signal
  3. gi.require_version('Gst', '1.0')
  4. from gi.repository import Gst, GObject
  5. # init GObject & Co. before importing local classes
  6. GObject.threads_init()
  7. Gst.init([])
  8. class LoopSource(object):
  9. def __init__(self):
  10. # it works much better with a local file
  11. pipeline = """
  12. uridecodebin name=src uri=http://c3voc.mazdermind.de/testfiles/bg.ts !
  13. videoscale !
  14. videoconvert !
  15. video/x-raw,format=UYVY,width=1920,height=1080,framerate=25/1,pixel-aspect-ratio=1/1 !
  16. matroskamux !
  17. tcpclientsink host=localhost port=16000
  18. """
  19. self.senderPipeline = Gst.parse_launch(pipeline)
  20. self.src = self.senderPipeline.get_by_name('src')
  21. # Binding End-of-Stream-Signal on Source-Pipeline
  22. self.senderPipeline.bus.add_signal_watch()
  23. self.senderPipeline.bus.connect("message::eos", self.on_eos)
  24. self.senderPipeline.bus.connect("message::error", self.on_error)
  25. print("playing")
  26. self.senderPipeline.set_state(Gst.State.PLAYING)
  27. def on_eos(self, bus, message):
  28. print('Received EOS-Signal, Seeking to start')
  29. self.src.seek(
  30. 1.0, # rate (float)
  31. Gst.Format.TIME, # format (Gst.Format)
  32. Gst.SeekFlags.FLUSH, # flags (Gst.SeekFlags)
  33. Gst.SeekType.SET, # start_type (Gst.SeekType)
  34. 0, # start (int)
  35. Gst.SeekType.NONE, # stop_type (Gst.SeekType)
  36. 0 # stop (int)
  37. )
  38. def on_error(self, bus, message):
  39. print('Received Error-Signal')
  40. (error, debug) = message.parse_error()
  41. print('Error-Details: #%u: %s' % (error.code, debug))
  42. sys.exit(1)
  43. def main():
  44. signal.signal(signal.SIGINT, signal.SIG_DFL)
  45. src = LoopSource()
  46. mainloop = GObject.MainLoop()
  47. try:
  48. mainloop.run()
  49. except KeyboardInterrupt:
  50. print('Terminated via Ctrl-C')
  51. if __name__ == '__main__':
  52. main()