summaryrefslogtreecommitdiff
path: root/example-scripts/gstreamer/source-remote-desktop-as-cam1.py
blob: 686cc73b3229b9590e4ce6c04c8fad811b5447c1 (plain)
  1. #!/usr/bin/python3
  2. import 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, ip):
  11. # it works much better with a local file
  12. pipeline = """
  13. ximagesrc use-damage=0 startx=0 starty=0 endx=1919 endy=1079 !
  14. queue !
  15. videoscale !
  16. videorate !
  17. timeoverlay !
  18. videoconvert !
  19. video/x-raw,format=I420,width=1280,height=720,framerate=25/1,pixel-aspect-ratio=1/1 !
  20. queue !
  21. mux.
  22. pulsesrc !
  23. audio/x-raw,format=S16LE,channels=2,layout=interleaved,rate=48000 !
  24. queue !
  25. mux.
  26. matroskamux name=mux !
  27. tcpclientsink host={ip} port=10000
  28. """.format(
  29. ip=ip
  30. )
  31. self.clock = GstNet.NetClientClock.new('voctocore', ip, 9998, 0)
  32. print('obtained NetClientClock from host', self.clock)
  33. print('waiting for NetClientClock to sync…')
  34. self.clock.wait_for_sync(Gst.CLOCK_TIME_NONE)
  35. print('starting pipeline')
  36. self.senderPipeline = Gst.parse_launch(pipeline)
  37. self.senderPipeline.use_clock(self.clock)
  38. self.src = self.senderPipeline.get_by_name('src')
  39. # Binding End-of-Stream-Signal on Source-Pipeline
  40. self.senderPipeline.bus.add_signal_watch()
  41. self.senderPipeline.bus.connect("message::eos", self.on_eos)
  42. self.senderPipeline.bus.connect("message::error", self.on_error)
  43. print("playing")
  44. self.senderPipeline.set_state(Gst.State.PLAYING)
  45. def on_eos(self, bus, message):
  46. print('Received EOS-Signal')
  47. sys.exit(1)
  48. def on_error(self, bus, message):
  49. print('Received Error-Signal')
  50. (error, debug) = message.parse_error()
  51. print('Error-Details: #%u: %s' % (error.code, debug))
  52. sys.exit(1)
  53. def main():
  54. signal.signal(signal.SIGINT, signal.SIG_DFL)
  55. parser = argparse.ArgumentParser(description='Voctocore Remote-Source')
  56. parser.add_argument('host')
  57. args = parser.parse_args()
  58. print('Resolving hostname '+args.host)
  59. addrs = [ str(i[4][0]) for i in socket.getaddrinfo(args.host, None) ]
  60. if len(addrs) == 0:
  61. print('Found no IPs')
  62. sys.exit(1)
  63. print('Using IP '+addrs[0])
  64. src = Source(addrs[0])
  65. mainloop = GObject.MainLoop()
  66. try:
  67. mainloop.run()
  68. except KeyboardInterrupt:
  69. print('Terminated via Ctrl-C')
  70. if __name__ == '__main__':
  71. main()