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