summaryrefslogtreecommitdiff
path: root/voctocore/lib/avsource.py
blob: c84ce85aaf5951fd370f106f990d5ce84e8492e7 (plain)
  1. #!/usr/bin/python3
  2. import logging
  3. from gi.repository import Gst
  4. from lib.config import Config
  5. from lib.tcpsingleconnection import TCPSingleConnection
  6. class AVSource(TCPSingleConnection):
  7. def __init__(self, name, port):
  8. self.log = logging.getLogger('AVSource['+name+']')
  9. super().__init__(port)
  10. self.name = name
  11. def on_accepted(self, conn, addr):
  12. pipeline = """
  13. fdsrc fd={fd} !
  14. matroskademux name=demux
  15. demux. !
  16. {acaps} !
  17. queue !
  18. tee name=atee
  19. atee. ! queue ! interaudiosink channel=audio_{name}_mixer
  20. atee. ! queue ! interaudiosink channel=audio_{name}_mirror
  21. """.format(
  22. fd=conn.fileno(),
  23. name=self.name,
  24. acaps=Config.get('mix', 'audiocaps')
  25. )
  26. if Config.getboolean('previews', 'enabled'):
  27. pipeline += """
  28. atee. ! queue ! interaudiosink channel=audio_{name}_preview
  29. """.format(
  30. name=self.name
  31. )
  32. pipeline += """
  33. demux. !
  34. {vcaps} !
  35. textoverlay halignment=left valignment=top ypad=25 text=AVSource !
  36. timeoverlay halignment=left valignment=top ypad=25 xpad=400 !
  37. queue !
  38. tee name=vtee
  39. vtee. ! queue ! intervideosink channel=video_{name}_mixer
  40. vtee. ! queue ! intervideosink channel=video_{name}_mirror
  41. """.format(
  42. fd=conn.fileno(),
  43. name=self.name,
  44. vcaps=Config.get('mix', 'videocaps')
  45. )
  46. if Config.getboolean('previews', 'enabled'):
  47. pipeline += """
  48. vtee. ! queue ! intervideosink channel=video_{name}_preview
  49. """.format(
  50. name=self.name
  51. )
  52. self.log.debug('Launching Source-Pipeline:\n%s', pipeline)
  53. self.receiverPipeline = Gst.parse_launch(pipeline)
  54. self.log.debug('Binding End-of-Stream-Signal on Source-Pipeline')
  55. self.receiverPipeline.bus.add_signal_watch()
  56. self.receiverPipeline.bus.connect("message::eos", self.on_eos)
  57. self.receiverPipeline.bus.connect("message::error", self.on_error)
  58. self.receiverPipeline.set_state(Gst.State.PLAYING)
  59. def on_eos(self, bus, message):
  60. self.log.debug('Received End-of-Stream-Signal on Source-Pipeline')
  61. if self.currentConnection is not None:
  62. self.disconnect()
  63. def on_error(self, bus, message):
  64. self.log.debug('Received Error-Signal on Source-Pipeline')
  65. (error, debug) = message.parse_error()
  66. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  67. if self.currentConnection is not None:
  68. self.disconnect()
  69. def disconnect(self):
  70. self.receiverPipeline.set_state(Gst.State.NULL)
  71. self.receiverPipeline = None
  72. self.close_connection()