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