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