summaryrefslogtreecommitdiff
path: root/voctocore/lib/avrawoutput.py
blob: ff50ecb7cfa73a5a25288ca5adde2bde3bccffef (plain)
  1. #!/usr/bin/python3
  2. import logging
  3. from gi.repository import Gst
  4. from lib.config import Config
  5. from lib.tcpmulticonnection import TCPMultiConnection
  6. class AVRawOutput(TCPMultiConnection):
  7. def __init__(self, channel, port):
  8. self.log = logging.getLogger('AVRawOutput['+channel+']')
  9. super().__init__(port)
  10. self.channel = channel
  11. pipeline = """
  12. interaudiosrc channel=audio_{channel} !
  13. {acaps} !
  14. queue !
  15. mux.
  16. intervideosrc channel=video_{channel} !
  17. {vcaps} !
  18. queue !
  19. mux.
  20. matroskamux
  21. name=mux
  22. streamable=true
  23. writing-app=Voctomix-AVRawOutput !
  24. multifdsink
  25. buffers-max=500
  26. sync-method=next-keyframe
  27. name=fd
  28. """.format(
  29. channel=self.channel,
  30. acaps=Config.get('mix', 'audiocaps'),
  31. vcaps=Config.get('mix', 'videocaps')
  32. )
  33. self.log.debug('Creating Output-Pipeline:\n%s', pipeline)
  34. self.outputPipeline = Gst.parse_launch(pipeline)
  35. self.log.debug('Binding Error & End-of-Stream-Signal on Output-Pipeline')
  36. self.outputPipeline.bus.add_signal_watch()
  37. self.outputPipeline.bus.connect("message::eos", self.on_eos)
  38. self.outputPipeline.bus.connect("message::error", self.on_error)
  39. self.log.debug('Launching Output-Pipeline')
  40. self.outputPipeline.set_state(Gst.State.PLAYING)
  41. def on_accepted(self, conn, addr):
  42. self.log.debug('Adding fd %u to multifdsink', conn.fileno())
  43. fdsink = self.outputPipeline.get_by_name('fd')
  44. fdsink.emit('add', conn.fileno())
  45. def on_disconnect(multifdsink, fileno):
  46. if fileno == conn.fileno():
  47. self.log.debug('fd %u removed from multifdsink', fileno)
  48. self.close_connection(conn)
  49. def on_about_to_disconnect(multifdsink, fileno, status):
  50. if fileno == conn.fileno() and status == 3: # Gst.MultiHandleSinkClientStatus.Slow
  51. self.log.warning('about to remove fd %u from multifdsink because it is too slow!', fileno)
  52. fdsink.connect('client-fd-removed', on_disconnect)
  53. fdsink.connect('client-removed', on_about_to_disconnect)
  54. def on_eos(self, bus, message):
  55. self.log.debug('Received End-of-Stream-Signal on Output-Pipeline')
  56. def on_error(self, bus, message):
  57. self.log.debug('Received Error-Signal on Output-Pipeline')
  58. (error, debug) = message.parse_error()
  59. self.log.debug('Error-Details: #%u: %s', error.code, debug)