summaryrefslogtreecommitdiff
path: root/voctocore/lib/avsource.py
blob: f814c4524f612ea52431e63f4a5f180f3a78e0e1 (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, 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.receiverPipeline.use_clock(Clock)
  58. self.log.debug('Binding End-of-Stream-Signal on Source-Pipeline')
  59. self.receiverPipeline.bus.add_signal_watch()
  60. self.receiverPipeline.bus.connect("message::eos", self.on_eos)
  61. self.receiverPipeline.bus.connect("message::error", self.on_error)
  62. self.all_video_caps = Gst.Caps.from_string('video/x-raw')
  63. self.video_caps = Gst.Caps.from_string(Config.get('mix', 'videocaps'))
  64. self.all_audio_caps = Gst.Caps.from_string('audio/x-raw')
  65. self.audio_caps = Gst.Caps.from_string(Config.get('mix', 'audiocaps'))
  66. demux = self.receiverPipeline.get_by_name('demux')
  67. demux.connect('pad-added', self.on_pad_added)
  68. self.receiverPipeline.set_state(Gst.State.PLAYING)
  69. def on_pad_added(self, demux, src_pad):
  70. caps = src_pad.query_caps(None)
  71. self.log.debug('demuxer added pad w/ caps: %s', caps.to_string())
  72. if caps.can_intersect(self.all_audio_caps):
  73. self.log.debug('new demuxer-pad is a audio-pad, testing against configured audio-caps')
  74. if not caps.can_intersect(self.audio_caps):
  75. self.log.warning('the incoming connection presented a video-stream that is not compatible to the configured caps')
  76. self.log.warning(' incoming caps: %s', caps.to_string())
  77. self.log.warning(' configured caps: %s', self.audio_caps.to_string())
  78. elif caps.can_intersect(self.all_video_caps):
  79. self.log.debug('new demuxer-pad is a video-pad, testing against configured video-caps')
  80. if not caps.can_intersect(self.video_caps):
  81. self.log.warning('the incoming connection presented a video-stream that is not compatible to the configured caps')
  82. self.log.warning(' incoming caps: %s', caps.to_string())
  83. self.log.warning(' configured caps: %s', self.video_caps.to_string())
  84. def on_eos(self, bus, message):
  85. self.log.debug('Received End-of-Stream-Signal on Source-Pipeline')
  86. if self.currentConnection is not None:
  87. self.disconnect()
  88. def on_error(self, bus, message):
  89. self.log.debug('Received Error-Signal on Source-Pipeline')
  90. (error, debug) = message.parse_error()
  91. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  92. if self.currentConnection is not None:
  93. self.disconnect()
  94. def disconnect(self):
  95. self.receiverPipeline.set_state(Gst.State.NULL)
  96. self.receiverPipeline = None
  97. self.close_connection()