aboutsummaryrefslogtreecommitdiff
path: root/example-scripts/gstreamer/source-remote-videotestsrc-as-cam1.py
blob: 5404addab720696c50f26b7449045f63fa1279ee (plain)
  1. #!/usr/bin/python3
  2. import os, sys, gi, signal
  3. import argparse, socket
  4. gi.require_version('Gst', '1.0')
  5. from gi.repository import Gst, GstNet, GObject
  6. # init GObject & Co. before importing local classes
  7. GObject.threads_init()
  8. Gst.init([])
  9. class Source(object):
  10. def __init__(self, settings):
  11. # it works much better with a local file
  12. pipeline = """
  13. videotestsrc pattern=ball foreground-color=0x00ff0000 background-color=0x00440000 !
  14. timeoverlay !
  15. video/x-raw,format=I420,width=1280,height=720,framerate=25/1,pixel-aspect-ratio=1/1 !
  16. mux.
  17. audiotestsrc freq=330 !
  18. audio/x-raw,format=S16LE,channels=2,layout=interleaved,rate=48000 !
  19. mux.
  20. matroskamux name=mux !
  21. tcpclientsink host={IP} port=10000
  22. """.format_map(settings)
  23. self.clock = GstNet.NetClientClock.new('voctocore', settings['IP'], 9998, 0)
  24. print('obtained NetClientClock from host', self.clock)
  25. print('waiting for NetClientClock to sync…')
  26. self.clock.wait_for_sync(Gst.CLOCK_TIME_NONE)
  27. print('starting pipeline '+pipeline)
  28. self.senderPipeline = Gst.parse_launch(pipeline)
  29. self.senderPipeline.use_clock(self.clock)
  30. self.src = self.senderPipeline.get_by_name('src')
  31. # Binding End-of-Stream-Signal on Source-Pipeline
  32. self.senderPipeline.bus.add_signal_watch()
  33. self.senderPipeline.bus.connect("message::eos", self.on_eos)
  34. self.senderPipeline.bus.connect("message::error", self.on_error)
  35. print("playing")
  36. self.senderPipeline.set_state(Gst.State.PLAYING)
  37. def on_eos(self, bus, message):
  38. print('Received EOS-Signal')
  39. sys.exit(1)
  40. def on_error(self, bus, message):
  41. print('Received Error-Signal')
  42. (error, debug) = message.parse_error()
  43. print('Error-Details: #%u: %s' % (error.code, debug))
  44. sys.exit(1)
  45. def main():
  46. signal.signal(signal.SIGINT, signal.SIG_DFL)
  47. parser = argparse.ArgumentParser(description='Voctocore Remote-Source')
  48. parser.add_argument('host')
  49. args = parser.parse_args()
  50. print('Resolving hostname '+args.host)
  51. addrs = [ str(i[4][0]) for i in socket.getaddrinfo(args.host, None) ]
  52. if len(addrs) == 0:
  53. print('Found no IPs')
  54. sys.exit(1)
  55. print('Using IP '+addrs[0])
  56. config = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../config.sh')
  57. with open(config) as config:
  58. lines = [ line.strip() for line in config if line[0] != '#' ]
  59. pairs = [ line.split('=', 1) for line in lines ]
  60. settings = { pair[0]: pair[1] for pair in pairs }
  61. settings['IP'] = addrs[0]
  62. src = Source(settings)
  63. mainloop = GObject.MainLoop()
  64. try:
  65. mainloop.run()
  66. except KeyboardInterrupt:
  67. print('Terminated via Ctrl-C')
  68. if __name__ == '__main__':
  69. main()