summaryrefslogtreecommitdiff
path: root/voctocore/lib/avsource.py
blob: 49b39a88b0b185cc060e2b6cbc99b4074f0ac954 (plain)
  1. #!/usr/bin/python3
  2. import logging, socket
  3. from gi.repository import GObject, Gst
  4. from lib.config import Config
  5. class AVSource(object):
  6. log = logging.getLogger('AVSource')
  7. name = None
  8. port = None
  9. caps = None
  10. receiverPipeline = None
  11. boundSocket = None
  12. currentConnection = None
  13. def __init__(self, name, port):
  14. self.log = logging.getLogger('AVSource['+name+']')
  15. self.name = name
  16. self.port = port
  17. self.log.debug('Binding to Source-Socket on [::]:%u', port)
  18. self.boundSocket = socket.socket(socket.AF_INET6)
  19. self.boundSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  20. self.boundSocket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
  21. self.boundSocket.bind(('::', port))
  22. self.boundSocket.listen(1)
  23. self.log.debug('Setting GObject io-watch on Socket')
  24. GObject.io_add_watch(self.boundSocket, GObject.IO_IN, self.on_connect)
  25. def on_connect(self, sock, *args):
  26. conn, addr = sock.accept()
  27. self.log.info("Incomming Connection from %s", addr)
  28. if self.currentConnection is not None:
  29. self.log.warn("Another Source is already connected")
  30. return True
  31. pipeline = """
  32. fdsrc fd={fd} !
  33. matroskademux name=demux
  34. demux. !
  35. {acaps} !
  36. queue !
  37. tee name=atee
  38. atee. ! queue ! interaudiosink channel=audio_{name}_mixer
  39. atee. ! queue ! interaudiosink channel=audio_{name}_mirror
  40. demux. !
  41. {vcaps} !
  42. textoverlay halignment=left valignment=top ypad=25 text=AVSource !
  43. timeoverlay halignment=left valignment=top ypad=25 xpad=400 !
  44. queue !
  45. tee name=vtee
  46. vtee. ! queue ! intervideosink channel=video_{name}_mixer
  47. vtee. ! queue ! intervideosink channel=video_{name}_mirror
  48. """.format(
  49. fd=conn.fileno(),
  50. name=self.name,
  51. acaps=Config.get('mix', 'audiocaps'),
  52. vcaps=Config.get('mix', 'videocaps')
  53. )
  54. self.log.debug('Launching Source-Pipeline:\n%s', pipeline)
  55. self.receiverPipeline = Gst.parse_launch(pipeline)
  56. self.log.debug('Binding End-of-Stream-Signal on Source-Pipeline')
  57. self.receiverPipeline.bus.add_signal_watch()
  58. self.receiverPipeline.bus.connect("message::eos", self.on_eos)
  59. self.receiverPipeline.bus.connect("message::error", self.on_error)
  60. self.receiverPipeline.set_state(Gst.State.PLAYING)
  61. self.currentConnection = conn
  62. return True
  63. def on_eos(self, bus, message):
  64. self.log.debug('Received End-of-Stream-Signal on Source-Pipeline')
  65. if self.currentConnection is not None:
  66. self.disconnect()
  67. def on_error(self, bus, message):
  68. self.log.debug('Received Error-Signal on Source-Pipeline')
  69. (error, debug) = message.parse_error()
  70. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  71. if self.currentConnection is not None:
  72. self.disconnect()
  73. def disconnect(self):
  74. self.log.info('Connection closed')
  75. self.receiverPipeline.set_state(Gst.State.NULL)
  76. self.receiverPipeline = None
  77. self.currentConnection = None