aboutsummaryrefslogtreecommitdiff
path: root/voctocore/experiments/intervideo.py
blob: 11b39ce3d470697249e4d5eda57db93e94048b97 (plain)
  1. #!/usr/bin/python3
  2. import gi, time
  3. import socket
  4. # import GStreamer and GTK-Helper classes
  5. gi.require_version('Gst', '1.0')
  6. from gi.repository import GLib, Gst, GObject
  7. # init GObject before importing local classes
  8. GObject.threads_init()
  9. Gst.init(None)
  10. class Example:
  11. def __init__(self):
  12. self.mainloop = GObject.MainLoop()
  13. self.source_pipeline = None
  14. self.sink_pipeline = Gst.parse_launch("""
  15. intervideosrc channel=video !
  16. queue !
  17. video/x-raw,width=800,height=450,format=I420,framerate=25/1 !
  18. textoverlay halignment=left valignment=top ypad=50 text=intervideosrc !
  19. timeoverlay halignment=left valignment=top ypad=50 xpad=400 !
  20. tee name=vtee
  21. interaudiosrc blocksize=4096 channel=audio !
  22. queue !
  23. audio/x-raw,format=S16LE,layout=interleaved,rate=48000,channels=2 !
  24. tee name=atee
  25. vtee. !
  26. queue !
  27. videoconvert !
  28. textoverlay halignment=left valignment=top ypad=75 text=avenc_mpeg2video !
  29. timeoverlay halignment=left valignment=top ypad=75 xpad=400 !
  30. avenc_mpeg2video bitrate=50000 max-key-interval=0 !
  31. queue !
  32. mux.
  33. atee. !
  34. queue !
  35. avenc_mp2 bitrate=192000 !
  36. queue !
  37. mux.
  38. mpegtsmux name=mux !
  39. filesink location=foo.ts
  40. vtee. !
  41. queue !
  42. textoverlay halignment=left valignment=top ypad=75 text=xvimagesink !
  43. timeoverlay halignment=left valignment=top ypad=75 xpad=400 !
  44. videoconvert !
  45. xvimagesink
  46. atee. !
  47. queue !
  48. audioconvert !
  49. alsasink
  50. """)
  51. # Create the server, binding to localhost on port 5000
  52. sock = socket.socket(socket.AF_INET6)
  53. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  54. sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
  55. sock.bind(('::', 10000))
  56. sock.listen(1)
  57. # register socket for callback inside the GTK-Mainloop
  58. GObject.io_add_watch(sock, GObject.IO_IN, self.on_connect)
  59. def on_connect(self, sock, *args):
  60. '''Asynchronous connection listener. Starts a handler for each connection.'''
  61. if self.source_pipeline:
  62. return False
  63. conn, addr = sock.accept()
  64. print("Connection from", addr)
  65. self.source_pipeline = Gst.parse_launch("""
  66. fdsrc name=a fd=%u !
  67. matroskademux name=demux
  68. demux. !
  69. video/x-raw,width=800,height=450,format=I420,framerate=25/1 !
  70. queue !
  71. textoverlay halignment=left valignment=top ypad=25 text=intervideosink !
  72. timeoverlay halignment=left valignment=top ypad=25 xpad=400 !
  73. intervideosink channel=video
  74. demux. !
  75. audio/x-raw,format=S16LE,channels=2,layout=interleaved,rate=48000,channel-mask=(bitmask)0x3 !
  76. queue !
  77. interaudiosink channel=audio
  78. """ % conn.fileno())
  79. self.source_pipeline.bus.add_signal_watch()
  80. self.source_pipeline.bus.connect("message::eos", self.on_disconnect)
  81. self.source_pipeline.set_state(Gst.State.PLAYING)
  82. self.conn = conn
  83. return True
  84. def on_disconnect(self, bus, message):
  85. self.source_pipeline.set_state(Gst.State.NULL)
  86. self.source_pipeline = None
  87. self.conn = None
  88. return True
  89. def run(self):
  90. self.sink_pipeline.set_state(Gst.State.PLAYING)
  91. self.mainloop.run()
  92. example = Example()
  93. example.run()