summaryrefslogtreecommitdiff
path: root/clients/source/ingest.py
blob: 446f1a6f8f28eb52028d4db4a86dc14b0815644f (plain)
  1. #!/usr/bin/env python3
  2. # ingest.py
  3. # source client for Voctomix
  4. import sys
  5. import gi
  6. import signal
  7. import os
  8. import socket
  9. import argparse
  10. gi.require_version('Gst', '1.0')
  11. from gi.repository import Gst, GstNet, GObject
  12. # init GObject & Co. before importing local classes
  13. GObject.threads_init()
  14. Gst.init([])
  15. # this is kinda icky.
  16. sys.path.insert(0, '../..' )
  17. sys.path.insert(0, '.' )
  18. import voctogui.lib.connection as Connection
  19. # import lib.clock as ClockManager
  20. def mk_video_src(args, videocaps):
  21. # make video soure part of pipeline
  22. video_device = "device={}".format(args.video_dev) \
  23. if args.video_dev else ""
  24. monitor = """tee name=t ! queue !
  25. videoconvert ! fpsdisplaysink sync=false
  26. t. ! queue !""" \
  27. if args.monitor else ""
  28. if args.video_source == 'dv':
  29. video_src = """
  30. dv1394src name=videosrc {video_device}!
  31. dvdemux name=demux !
  32. queue !
  33. dvdec !
  34. {monitor}
  35. deinterlace mode=1 !
  36. videoconvert !
  37. videorate !
  38. videoscale !
  39. """
  40. elif args.video_source == 'hdv':
  41. video_src = """
  42. hdv1394src {video_device} do-timestamp=true name=videosrc !
  43. tsdemux name=demux!
  44. queue !
  45. decodebin !
  46. {monitor}
  47. deinterlace mode=1 !
  48. videorate !
  49. videoscale !
  50. videoconvert !
  51. """
  52. elif args.video_source == 'hdmi2usb':
  53. video_src = """
  54. v4l2src {video_device} name=videosrc !
  55. queue !
  56. image/jpeg,width=1280,height=720 !
  57. jpegdec !
  58. {monitor}
  59. videoconvert !
  60. videorate !
  61. """
  62. elif args.video_source == 'ximage':
  63. video_src = """
  64. ximagesrc name=videosrc
  65. use-damage=false !
  66. {monitor}
  67. videoconvert !
  68. videorate !
  69. videoscale !
  70. """
  71. # startx=0 starty=0 endx=1919 endy=1079 !
  72. elif args.video_source == 'blackmagichdmi':
  73. video_src = """
  74. decklinkvideosrc mode=17 connection=2 !
  75. {monitor}
  76. videoconvert !
  77. videorate !
  78. videoscale !
  79. """
  80. elif args.video_source == 'test':
  81. video_src = """
  82. videotestsrc name=videosrc
  83. pattern=ball
  84. foreground-color=0x00ff0000 background-color=0x00440000 !
  85. {monitor}
  86. """
  87. video_src = video_src.format(
  88. video_device=video_device,
  89. monitor=monitor)
  90. video_src += videocaps + "!\n"
  91. return video_src
  92. def mk_audio_src(args, audiocaps):
  93. audio_device = "device={}".format(args.audio_dev) \
  94. if args.audio_dev else ""
  95. if args.audio_source in [ 'dv', 'hdv' ]:
  96. # this only works if video is from DV also.
  97. # or some gst source that gets demux ed
  98. audio_src = """
  99. demux. !
  100. audioconvert !
  101. """
  102. elif args.audio_source == 'pulse':
  103. audio_src = """
  104. pulsesrc {audio_device} name=audiosrc !
  105. """.format(audio_device=audio_device)
  106. elif args.audio_source == 'blackmagichdmi':
  107. audio_src = """
  108. decklinkaudiosrc !
  109. """
  110. elif args.audio_source == 'test':
  111. audio_src = """
  112. audiotestsrc name=audiosrc freq=330 !
  113. """
  114. audio_src += audiocaps + "!\n"
  115. return audio_src
  116. def mk_mux(args):
  117. mux = """
  118. mux.
  119. matroskamux name=mux !
  120. """
  121. return mux
  122. def mk_client(args):
  123. core_ip = socket.gethostbyname(args.host)
  124. client = """
  125. tcpclientsink host={host} port={port}
  126. """.format(host=core_ip, port=args.port)
  127. return client
  128. def mk_pipeline(args, server_caps):
  129. video_src = mk_video_src(args, server_caps['videocaps'])
  130. audio_src = mk_audio_src(args, server_caps['audiocaps'])
  131. mux = mk_mux(args)
  132. client = mk_client(args)
  133. pipeline = video_src + "mux.\n" + audio_src + mux + client
  134. # remove blank lines to make it more human readable
  135. pipeline = pipeline.replace("\n\n","\n")
  136. return pipeline
  137. def get_server_caps():
  138. # fetch config from server
  139. server_config = Connection.fetchServerConfig()
  140. server_caps = {'videocaps': server_config['mix']['videocaps'],
  141. 'audiocaps': server_config['mix']['audiocaps']}
  142. return server_caps
  143. # obtain network-clock
  144. ClockManager.obtainClock(Connection.ip)
  145. def run_pipeline(pipeline, args):
  146. core_ip = socket.gethostbyname(args.host)
  147. clock = GstNet.NetClientClock.new('voctocore', core_ip, 9998, 0)
  148. print('obtained NetClientClock from host', clock)
  149. print('waiting for NetClientClock to sync…')
  150. clock.wait_for_sync(Gst.CLOCK_TIME_NONE)
  151. print('starting pipeline')
  152. senderPipeline = Gst.parse_launch(pipeline)
  153. senderPipeline.use_clock(clock)
  154. src = senderPipeline.get_by_name('src')
  155. def on_eos(self, bus, message):
  156. print('Received EOS-Signal')
  157. sys.exit(1)
  158. def on_error(self, bus, message):
  159. print('Received Error-Signal')
  160. (error, debug) = message.parse_error()
  161. print('Error-Details: #%u: %s' % (error.code, debug))
  162. sys.exit(1)
  163. # Binding End-of-Stream-Signal on Source-Pipeline
  164. senderPipeline.bus.add_signal_watch()
  165. senderPipeline.bus.connect("message::eos", on_eos)
  166. senderPipeline.bus.connect("message::error", on_error)
  167. print("playing")
  168. senderPipeline.set_state(Gst.State.PLAYING)
  169. mainloop = GObject.MainLoop()
  170. try:
  171. mainloop.run()
  172. except KeyboardInterrupt:
  173. print('Terminated via Ctrl-C')
  174. def get_args():
  175. parser = argparse.ArgumentParser(description='Vocto-ingest client')
  176. parser.add_argument('-v', '--verbose', action='count', default=0,
  177. help="Also print INFO and DEBUG messages.")
  178. parser.add_argument( '--video-source', action='store',
  179. choices=[
  180. 'dv', 'hdv', 'hdmi2usb', 'blackmagichdmi',
  181. 'ximage',
  182. 'test', ],
  183. default='test',
  184. help="Where to get video from")
  185. parser.add_argument( '--video-dev', action='store',
  186. help="video device")
  187. parser.add_argument( '--audio-source', action='store',
  188. choices=['dv', 'alsa', 'pulse', 'blackmagichdmi', 'test'],
  189. default='test',
  190. help="Where to get audio from")
  191. parser.add_argument( '--audio-dev', action='store',
  192. default='hw:CARD=CODEC',
  193. help="for alsa/pulse, audio device")
  194. # maybe hw:1,0
  195. parser.add_argument( '--audio-delay', action='store',
  196. default='10',
  197. help="ms to delay audio")
  198. parser.add_argument('-m', '--monitor', action='store_true',
  199. help="fps display sink")
  200. parser.add_argument( '--host', action='store',
  201. default='localhost',
  202. help="hostname of vocto core")
  203. parser.add_argument( '--port', action='store',
  204. default='10000',
  205. help="port of vocto core")
  206. args = parser.parse_args()
  207. return args
  208. def main():
  209. args = get_args()
  210. core_ip = socket.gethostbyname(args.host)
  211. # establish a synchronus connection to server
  212. Connection.establish(core_ip)
  213. server_caps = get_server_caps()
  214. pipeline = mk_pipeline(args, server_caps)
  215. print(pipeline)
  216. run_pipeline(pipeline, args)
  217. if __name__ == '__main__':
  218. main()