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