summaryrefslogtreecommitdiff
path: root/voctocore/lib/vsource.py
blob: 893c7c0c91e000891231586c8a3e9abe468139fa (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 VSource(TCPSingleConnection):
  7. def __init__(self, name, port):
  8. self.log = logging.getLogger('VSource['+name+']')
  9. super().__init__(port)
  10. self.name = name
  11. def on_accepted(self, conn, addr):
  12. pipeline = """
  13. fdsrc fd={fd} !
  14. matroskademux !
  15. {vcaps} !
  16. intervideosink channel=video_{name}
  17. """.format(
  18. fd=conn.fileno(),
  19. name=self.name,
  20. vcaps=Config.get('mix', 'videocaps')
  21. )
  22. self.log.debug('Launching Source-Pipeline:\n%s', pipeline)
  23. self.receiverPipeline = Gst.parse_launch(pipeline)
  24. self.log.debug('Binding End-of-Stream-Signal on Source-Pipeline')
  25. self.receiverPipeline.bus.add_signal_watch()
  26. self.receiverPipeline.bus.connect("message::eos", self.on_eos)
  27. self.receiverPipeline.bus.connect("message::error", self.on_error)
  28. self.all_video_caps = Gst.Caps.from_string('video/x-raw')
  29. self.video_caps = Gst.Caps.from_string(Config.get('mix', 'videocaps'))
  30. demux = self.receiverPipeline.get_by_name('demux')
  31. demux.connect('pad-added', self.on_pad_added)
  32. self.receiverPipeline.set_state(Gst.State.PLAYING)
  33. def on_pad_added(self, demux, src_pad):
  34. caps = src_pad.query_caps(None)
  35. self.log.debug('demuxer added pad w/ caps: %s', caps.to_string())
  36. if caps.can_intersect(self.all_video_caps):
  37. self.log.debug('new demuxer-pad is a video-pad, testing against configured video-caps')
  38. if not caps.can_intersect(self.video_caps):
  39. self.log.warning('the incoming connection presented a video-stream that is not compatible to the configured caps')
  40. self.log.warning(' incoming caps: %s', caps.to_string())
  41. self.log.warning(' configured caps: %s', self.video_caps.to_string())
  42. def on_eos(self, bus, message):
  43. self.log.debug('Received End-of-Stream-Signal on Source-Pipeline')
  44. if self.currentConnection is not None:
  45. self.disconnect()
  46. def on_error(self, bus, message):
  47. self.log.debug('Received Error-Signal on Source-Pipeline')
  48. (error, debug) = message.parse_error()
  49. self.log.debug('Error-Details: #%u: %s', error.code, debug)
  50. if self.currentConnection is not None:
  51. self.disconnect()
  52. def disconnect(self):
  53. self.receiverPipeline.set_state(Gst.State.NULL)
  54. self.receiverPipeline = None
  55. self.close_connection()