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