summaryrefslogtreecommitdiff
path: root/voctocore/lib/avpreviewoutput.py
blob: b05457a06ff8b4de33bd33aaab23e4ee44c67ecf (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 AVPreviewOutput(TCPMultiConnection):
  7. def __init__(self, channel, port):
  8. self.log = logging.getLogger('AVPreviewOutput['+channel+']')
  9. super().__init__(port)
  10. self.channel = channel
  11. if Config.has_option('previews', 'videocaps'):
  12. vcaps_out = Config.get('previews', 'videocaps')
  13. else:
  14. vcaps_out = Config.get('mix', 'videocaps')
  15. pipeline = """
  16. intervideosrc channel=video_{channel} !
  17. {vcaps_in} !
  18. deinterlace mode=interlaced !
  19. videoscale !
  20. videorate !
  21. {vcaps_out} !
  22. jpegenc quality=90 !
  23. queue !
  24. mux.
  25. interaudiosrc channel=audio_{channel} !
  26. {acaps} !
  27. queue !
  28. mux.
  29. matroskamux
  30. name=mux
  31. streamable=true
  32. writing-app=Voctomix-AVPreviewOutput !
  33. multifdsink
  34. blocksize=1048576
  35. buffers-max=500
  36. sync-method=next-keyframe
  37. name=fd
  38. """.format(
  39. channel=self.channel,
  40. acaps=Config.get('mix', 'audiocaps'),
  41. vcaps_in=Config.get('mix', 'videocaps'),
  42. vcaps_out=vcaps_out
  43. )
  44. self.log.debug('Creating Output-Pipeline:\n%s', pipeline)
  45. self.outputPipeline = Gst.parse_launch(pipeline)
  46. self.outputPipeline.use_clock(Clock)
  47. self.log.debug('Binding Error & End-of-Stream-Signal on Output-Pipeline')
  48. self.outputPipeline.bus.add_signal_watch()
  49. self.outputPipeline.bus.connect("message::eos", self.on_eos)
  50. self.outputPipeline.bus.connect("message::error", self.on_error)
  51. self.log.debug('Launching Output-Pipeline')
  52. self.outputPipeline.set_state(Gst.State.PLAYING)
  53. def on_accepted(self, conn, addr):
  54. self.log.debug('Adding fd %u to multifdsink', conn.fileno())
  55. fdsink = self.outputPipeline.get_by_name('fd')
  56. fdsink.emit('add', conn.fileno())
  57. def on_disconnect(multifdsink, fileno):
  58. if fileno == conn.fileno():
  59. self.log.debug('fd %u removed from multifdsink', fileno)
  60. self.close_connection(conn)
  61. fdsink.connect('client-fd-removed', on_disconnect)
  62. def on_eos(self, bus, message):
  63. self.log.debug('Received End-of-Stream-Signal on Output-Pipeline')
  64. def on_error(self, bus, message):
  65. self.log.debug('Received Error-Signal on Output-Pipeline')
  66. (error, debug) = message.parse_error()
  67. self.log.debug('Error-Details: #%u: %s', error.code, debug)