aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/avrawoutput.py
blob: 70243bd8c82dd0a0b08cbf5ef9827b78e8b9cf5d (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.bus.add_signal_watch()
  43. self.receiverPipeline.set_state(Gst.State.PLAYING)
  44. self.log.debug('Binding to Output-Socket on [::]:%u', port)
  45. self.boundSocket = socket.socket(socket.AF_INET6)
  46. self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  47. self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
  48. self.boundSocket.bind(('::', port))
  49. self.boundSocket.listen(1)
  50. self.log.debug('Setting GObject io-watch on Socket')
  51. GObject.io_add_watch(self.boundSocket, GObject.IO_IN, self.on_connect)
  52. def on_connect(self, sock, *args):
  53. conn, addr = sock.accept()
  54. self.log.info("Incomming Connection from %s", addr)
  55. self.log.info('Adding fd %u to multifdsink', conn.fileno())
  56. self.receiverPipeline.get_by_name('fd').emit('add', conn.fileno())
  57. self.currentConnections.append(conn)
  58. self.log.info('Now %u Receiver connected', len(self.currentConnections))
  59. return True
  60. # FIXME handle disconnects
  61. def disconnect(self, receiverPipeline, currentConnection):
  62. self.currentConnections.remove(currentConnection)
  63. self.log.info('Disconnected Receiver, now %u Receiver connected', len(self.currentConnections))