summaryrefslogtreecommitdiff
path: root/voctocore/lib/asource.py
blob: 6cf6029b66912419efdffec0c79be5f8b8953a70 (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 ASource(TCPSingleConnection):
  7. def __init__(self, name, port):
  8. self.log = logging.getLogger('ASource['+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. {acaps} !
  16. interaudiosink channel=audio_{name}
  17. """.format(
  18. fd=conn.fileno(),
  19. name=self.name,
  20. acaps=Config.get('mix', 'audiocaps')
  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_audio_caps = Gst.Caps.from_string('audio/x-raw')
  29. self.audio_caps = Gst.Caps.from_string(Config.get('mix', 'audiocaps'))
  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_audio_caps):
  37. self.log.debug('new demuxer-pad is a audio-pad, testing against configured audio-caps')
  38. if not caps.can_intersect(self.audio_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.audio_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()