summaryrefslogtreecommitdiff
path: root/voctocore/experiments/intervideo.py
blob: 404ea203c4547e71901f675ce077d7c8a626232f (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.vsink = Gst.parse_launch('intervideosrc channel=video ! video/x-raw,height=600,width=800,format=I420,framerate=25/1 ! timeoverlay ! videoconvert ! ximagesink')
  14. self.vsource = None
  15. #self.asink = Gst.parse_launch('interaudiosrc channel=audio ! audio/x-raw,format=S16LE,layout=interleaved,rate=48000,channels=2 ! autoaudiosink')
  16. self.asource = None
  17. self.sink = Gst.parse_launch("""
  18. intervideosrc channel=video !
  19. queue !
  20. video/x-raw,height=600,width=800,format=I420,framerate=25/1 !
  21. timeoverlay !
  22. tee name=vtee !
  23. queue !
  24. videoconvert !
  25. avenc_mpeg2video bitrate=50000 max-key-interval=0 !
  26. queue !
  27. mux.
  28. interaudiosrc blocksize=4096 channel=audio !
  29. queue !
  30. audio/x-raw,format=S16LE,layout=interleaved,rate=48000,channels=2 !
  31. tee name=atee !
  32. queue !
  33. avenc_mp2 bitrate=192000 !
  34. queue !
  35. mux.
  36. mpegtsmux name=mux !
  37. filesink location=foo.ts
  38. vtee. ! queue ! videoconvert ! xvimagesink
  39. atee. ! queue ! audioconvert ! alsasink
  40. """)
  41. # Create the server, binding to localhost on port 5000
  42. vsock = socket.socket(socket.AF_INET6)
  43. vsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  44. vsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
  45. vsock.bind(('::', 5000))
  46. vsock.listen(1)
  47. # Create the server, binding to localhost on port 5000
  48. asock = socket.socket(socket.AF_INET6)
  49. asock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  50. asock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
  51. asock.bind(('::', 6000))
  52. asock.listen(1)
  53. # register socket for callback inside the GTK-Mainloop
  54. GObject.io_add_watch(vsock, GObject.IO_IN, self.video_connect)
  55. GObject.io_add_watch(asock, GObject.IO_IN, self.audio_connect)
  56. def video_connect(self, sock, *args):
  57. '''Asynchronous connection listener. Starts a handler for each connection.'''
  58. if self.vsource:
  59. return False
  60. conn, addr = sock.accept()
  61. print("Connection from", addr)
  62. self.vsource = Gst.parse_launch('fdsrc name=a fd=%u ! gdpdepay ! video/x-raw,height=600,width=800,format=I420,framerate=25/1 ! timeoverlay halignment=right ! intervideosink channel=video' % conn.fileno())
  63. self.vsource.bus.add_signal_watch()
  64. self.vsource.bus.connect("message::eos", self.video_disconnect)
  65. self.vsource.set_state(Gst.State.PLAYING)
  66. self.vconn = conn
  67. return True
  68. def video_disconnect(self, bus, message):
  69. self.vsource.set_state(Gst.State.NULL)
  70. self.vsource = None
  71. self.vconn = None
  72. return True
  73. def audio_connect(self, sock, *args):
  74. '''Asynchronous connection listener. Starts a handler for each connection.'''
  75. if self.asource:
  76. return False
  77. conn, addr = sock.accept()
  78. print("Connection from", addr)
  79. self.asource = Gst.parse_launch('fdsrc name=a fd=%u ! gdpdepay ! audio/x-raw,format=S16LE,layout=interleaved,rate=48000,channels=2 ! interaudiosink channel=audio' % conn.fileno())
  80. self.asource.bus.add_signal_watch()
  81. self.asource.bus.connect("message::eos", self.audio_disconnect)
  82. self.asource.set_state(Gst.State.PLAYING)
  83. self.aconn = conn
  84. return True
  85. def audio_disconnect(self, bus, message):
  86. self.asource.set_state(Gst.State.NULL)
  87. self.asource = None
  88. self.aconn = None
  89. return True
  90. def run(self):
  91. self.sink.set_state(Gst.State.PLAYING)
  92. self.mainloop.run()
  93. def kill(self):
  94. self.sink.set_state(Gst.State.NULL)
  95. self.mainloop.quit()
  96. example = Example()
  97. example.run()