summaryrefslogtreecommitdiff
path: root/voctocore/lib/avrawoutput.py
blob: fd16e589f71d0f6fc300fbbf25493555c27e0de7 (plain)
  1. #!/usr/bin/python3
  2. import logging, socket
  3. from gi.repository import GObject, Gst
  4. from lib.config import Config
  5. class AVRawOutput(object):
  6. log = logging.getLogger('AVRawOutput')
  7. name = None
  8. port = None
  9. caps = None
  10. boundSocket = None
  11. receiverPipeline = None
  12. currentConnections = []
  13. def __init__(self, channel, port):
  14. self.log = logging.getLogger('AVRawOutput['+channel+']')
  15. self.channel = channel
  16. self.port = port
  17. pipeline = """
  18. interaudiosrc channel=audio_{channel} !
  19. {acaps} !
  20. queue !
  21. mux.
  22. intervideosrc channel=video_{channel} !
  23. {vcaps} !
  24. textoverlay halignment=left valignment=top ypad=75 text=AVRawOutput !
  25. timeoverlay halignment=left valignment=top ypad=75 xpad=400 !
  26. queue !
  27. mux.
  28. matroskamux
  29. name=mux
  30. streamable=true
  31. writing-app=Voctomix-AVRawOutput !
  32. multifdsink
  33. sync-method=next-keyframe
  34. name=fd
  35. """.format(
  36. channel=self.channel,
  37. acaps=Config.get('mix', 'audiocaps'),
  38. vcaps=Config.get('mix', 'videocaps')
  39. )
  40. self.log.debug('Launching Output-Pipeline:\n%s', pipeline)
  41. self.receiverPipeline = Gst.parse_launch(pipeline)
  42. self.receiverPipeline.set_state(Gst.State.PLAYING)
  43. self.log.debug('Binding to Output-Socket on [::]:%u', port)
  44. self.boundSocket = socket.socket(socket.AF_INET6)
  45. self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  46. self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
  47. self.boundSocket.bind(('::', port))
  48. self.boundSocket.listen(1)
  49. self.log.debug('Setting GObject io-watch on Socket')
  50. GObject.io_add_watch(self.boundSocket, GObject.IO_IN, self.on_connect)
  51. def on_connect(self, sock, *args):
  52. conn, addr = sock.accept()
  53. self.log.info("Incomming Connection from %s", addr)
  54. def on_disconnect(multifdsink, fileno):
  55. if fileno == conn.fileno():
  56. self.log.debug('fd %u removed from multifdsink', fileno)
  57. self.currentConnections.remove(conn)
  58. self.log.info('Disconnected Receiver %s, now %u Receiver connected', addr, len(self.currentConnections))
  59. self.log.debug('Adding fd %u to multifdsink', conn.fileno())
  60. fdsink = self.receiverPipeline.get_by_name('fd')
  61. fdsink.emit('add', conn.fileno())
  62. fdsink.connect('client-fd-removed', on_disconnect)
  63. self.currentConnections.append(conn)
  64. self.log.info('Now %u Receiver connected', len(self.currentConnections))
  65. return True