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