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