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