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