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