summaryrefslogtreecommitdiff
path: root/example-scripts/gstreamer/source-background-loop.py
blob: 2f13edff8837f6010995c6784dc92c8df228d919 (plain)
  1. #!/usr/bin/env python3
  2. import os, 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, settings):
  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=I420,width={WIDTH},height={HEIGHT},framerate={FRAMERATE}/1,pixel-aspect-ratio=1/1 !
  16. matroskamux !
  17. tcpclientsink host=localhost port=16000
  18. """.format_map(settings)
  19. print('starting pipeline '+pipeline)
  20. self.senderPipeline = Gst.parse_launch(pipeline)
  21. self.src = self.senderPipeline.get_by_name('src')
  22. # Binding End-of-Stream-Signal on Source-Pipeline
  23. self.senderPipeline.bus.add_signal_watch()
  24. self.senderPipeline.bus.connect("message::eos", self.on_eos)
  25. self.senderPipeline.bus.connect("message::error", self.on_error)
  26. print("playing")
  27. self.senderPipeline.set_state(Gst.State.PLAYING)
  28. def on_eos(self, bus, message):
  29. print('Received EOS-Signal, Seeking to start')
  30. self.src.seek(
  31. 1.0, # rate (float)
  32. Gst.Format.TIME, # format (Gst.Format)
  33. Gst.SeekFlags.FLUSH, # flags (Gst.SeekFlags)
  34. Gst.SeekType.SET, # start_type (Gst.SeekType)
  35. 0, # start (int)
  36. Gst.SeekType.NONE, # stop_type (Gst.SeekType)
  37. 0 # stop (int)
  38. )
  39. def on_error(self, bus, message):
  40. print('Received Error-Signal')
  41. (error, debug) = message.parse_error()
  42. print('Error-Details: #%u: %s' % (error.code, debug))
  43. sys.exit(1)
  44. def main():
  45. signal.signal(signal.SIGINT, signal.SIG_DFL)
  46. config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../config.sh')
  47. with open(config, 'r') as config:
  48. lines = [ line.strip() for line in config if line[0] != '#' ]
  49. pairs = [ line.split('=', 1) for line in lines ]
  50. settings = { pair[0]: pair[1] for pair in pairs }
  51. src = LoopSource(settings)
  52. mainloop = GObject.MainLoop()
  53. try:
  54. mainloop.run()
  55. except KeyboardInterrupt:
  56. print('Terminated via Ctrl-C')
  57. if __name__ == '__main__':
  58. main()