aboutsummaryrefslogtreecommitdiff
path: root/voctocore/lib/avsource.py
blob: ecd5b0f0b617e60a8edc08ad38e020973ab241b8 (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. queue !
  36. tee name=vtee
  37. vtee. ! queue ! intervideosink channel=video_{name}_mixer
  38. vtee. ! queue ! intervideosink channel=video_{name}_mirror
  39. """.format(
  40. fd=conn.fileno(),
  41. name=self.name,
  42. vcaps=Config.get('mix', 'videocaps')
  43. )
  44. if Config.getboolean('previews', 'enabled'):
  45. pipeline += """
  46. vtee. ! queue ! intervideosink channel=video_{name}_preview
  47. """.format(
  48. name=self.name
  49. )
  50. self.log.debug('Launching Source-Pipeline:\n%s', pipeline)
  51. self.receiverPipeline = Gst.parse_launch(pipeline)
  52. self.log.debug('Binding End-of-Stream-Signal on Source-Pipeline')
  53. self.receiverPipeline.bus.add_signal_watch()
  54. self.receiverPipeline.bus.connect("message::eos", self.on_eos)
  55. self.receiverPipeline.bus.connect("message::error", self.on_error)
  56. self.all_video_caps = Gst.Caps.from_string('video/x-raw')
  57. self.video_caps = Gst.Caps.from_string(Config.get('mix', 'videocaps'))
  58. self.all_audio_caps = Gst.Caps.from_string('audio/x-raw')
  59. self.audio_caps = Gst.Caps.from_string(Config.get('mix', 'audiocaps'))
  60. demux = self.receiverPipeline.get_by_name('demux')
  61. demux.connect('pad-added', self.on_pad_added)
  62. self.receiverPipeline.set_state(Gst.State.PLAYING)
  63. def on_pad_added(self, demux, src_pad):
  64. caps = src_pad.query_caps(None)
  65. self.log.debug('demuxer added pad w/ caps: %s', caps.to_string())
  66. if caps.can_intersect(self.all_audio_caps):
  67. self.log.debug('new demuxer-pad is a audio-pad, testing against configured audio-caps')
  68. if not caps.can_intersect(self.audio_caps):
  69. self.log.warning('the incoming connection presented a video-stream that is not compatible to the configured caps')
  70. self.log.warning(' incoming caps: %s', caps.to_string())
  71. self.log.warning(' configured caps: %s', self.audio_caps.to_string())
  72. elif caps.can_intersect(self.all_video_caps):
  73. self.log.debug('new demuxer-pad is a video-pad, testing against configured video-caps')
  74. if not caps.can_intersect(self.video_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.video_caps.to_string())
  78. def on_eos(self, bus, message):
  79. self.log.debug('Received End-of-Stream-Signal on Source-Pipeline')
  80. if self.currentConnection is not None:
  81. self.disconnect()
  82. def on_error(self, bus, message):
  83. self.log.debug('Received Error-Signal on Source-Pipeline')
  84. (error, debug) = message.parse_error()
  85. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  86. if self.currentConnection is not None:
  87. self.disconnect()
  88. def disconnect(self):
  89. self.receiverPipeline.set_state(Gst.State.NULL)
  90. self.receiverPipeline = None
  91. self.close_connection()