summaryrefslogtreecommitdiff
path: root/voctocore/lib/avsource.py
blob: 068b07fc0729572b9ecc8cd78ee70d1572d4dbc5 (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, outputs=None, has_audio=True, has_video=True):
  8. self.log = logging.getLogger('AVSource['+name+']')
  9. super().__init__(port)
  10. if outputs is None:
  11. outputs = [name]
  12. assert has_audio or has_video
  13. self.name = name
  14. self.has_audio = has_audio
  15. self.has_video = has_video
  16. self.outputs = outputs
  17. def on_accepted(self, conn, addr):
  18. pipeline = """
  19. fdsrc fd={fd} !
  20. queue !
  21. matroskademux name=demux
  22. """.format(
  23. fd=conn.fileno()
  24. )
  25. if self.has_audio:
  26. pipeline += """
  27. demux. !
  28. {acaps} !
  29. queue !
  30. tee name=atee
  31. """.format(
  32. acaps=Config.get('mix', 'audiocaps')
  33. )
  34. for output in self.outputs:
  35. pipeline += """
  36. atee. ! queue ! interaudiosink channel=audio_{output}
  37. """.format(
  38. output=output
  39. )
  40. if self.has_video:
  41. pipeline += """
  42. demux. !
  43. {vcaps} !
  44. queue !
  45. tee name=vtee
  46. """.format(
  47. vcaps=Config.get('mix', 'videocaps')
  48. )
  49. for output in self.outputs:
  50. pipeline += """
  51. vtee. ! queue ! intervideosink channel=video_{output}
  52. """.format(
  53. output=output
  54. )
  55. self.log.debug('Launching Source-Pipeline:\n%s', pipeline)
  56. self.receiverPipeline = Gst.parse_launch(pipeline)
  57. self.log.debug('Binding End-of-Stream-Signal on Source-Pipeline')
  58. self.receiverPipeline.bus.add_signal_watch()
  59. self.receiverPipeline.bus.connect("message::eos", self.on_eos)
  60. self.receiverPipeline.bus.connect("message::error", self.on_error)
  61. self.all_video_caps = Gst.Caps.from_string('video/x-raw')
  62. self.video_caps = Gst.Caps.from_string(Config.get('mix', 'videocaps'))
  63. self.all_audio_caps = Gst.Caps.from_string('audio/x-raw')
  64. self.audio_caps = Gst.Caps.from_string(Config.get('mix', 'audiocaps'))
  65. demux = self.receiverPipeline.get_by_name('demux')
  66. demux.connect('pad-added', self.on_pad_added)
  67. self.receiverPipeline.set_state(Gst.State.PLAYING)
  68. def on_pad_added(self, demux, src_pad):
  69. caps = src_pad.query_caps(None)
  70. self.log.debug('demuxer added pad w/ caps: %s', caps.to_string())
  71. if caps.can_intersect(self.all_audio_caps):
  72. self.log.debug('new demuxer-pad is a audio-pad, testing against configured audio-caps')
  73. if not caps.can_intersect(self.audio_caps):
  74. self.log.warning('the incoming connection presented a video-stream that is not compatible to the configured caps')
  75. self.log.warning(' incoming caps: %s', caps.to_string())
  76. self.log.warning(' configured caps: %s', self.audio_caps.to_string())
  77. elif caps.can_intersect(self.all_video_caps):
  78. self.log.debug('new demuxer-pad is a video-pad, testing against configured video-caps')
  79. if not caps.can_intersect(self.video_caps):
  80. self.log.warning('the incoming connection presented a video-stream that is not compatible to the configured caps')
  81. self.log.warning(' incoming caps: %s', caps.to_string())
  82. self.log.warning(' configured caps: %s', self.video_caps.to_string())
  83. def on_eos(self, bus, message):
  84. self.log.debug('Received End-of-Stream-Signal on Source-Pipeline')
  85. if self.currentConnection is not None:
  86. self.disconnect()
  87. def on_error(self, bus, message):
  88. self.log.debug('Received Error-Signal on Source-Pipeline')
  89. (error, debug) = message.parse_error()
  90. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  91. if self.currentConnection is not None:
  92. self.disconnect()
  93. def disconnect(self):
  94. self.receiverPipeline.set_state(Gst.State.NULL)
  95. self.receiverPipeline = None
  96. self.close_connection()