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