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