forked from CarlFK/voctomix-outcasts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingest.py
executable file
·344 lines (259 loc) · 8.88 KB
/
ingest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env python3
# ingest.py
"""
Source client for Voctomix.
Features:
Retrieves audio and video-caps config from core.
Uses core's clock.
Mix and match audio and video sources muxed into one streem.
Can display video locally, including frame count and fps.
Defaults to test audio and video sent to local core.
"""
import argparse
import gi
import os
import signal
import socket
import sys
gi.require_version('Gst', '1.0')
gi.require_version('GstNet', '1.0')
from gi.repository import Gst, GstNet, GObject
# init GObject & Co. before importing local classes
GObject.threads_init()
Gst.init([])
import lib.connection as Connection
def mk_video_src(args, videocaps):
# make video soure part of pipeline
d={ 'attribs': args.video_attribs }
d['monitor'] = """tee name=t ! queue !
videoconvert ! fpsdisplaysink sync=false
t. ! queue !""" \
if args.monitor else ""
if args.video_source == 'dv':
video_src = """
dv1394src name=videosrc {attribs} !
dvdemux name=demux !
queue !
dvdec !
{monitor}
deinterlace mode=1 !
videoconvert !
videorate !
videoscale !
"""
elif args.video_source == 'hdv':
video_src = """
hdv1394src {attribs} do-timestamp=true name=videosrc !
tsdemux name=demux!
queue !
decodebin !
{monitor}
deinterlace mode=1 !
videorate !
videoscale !
videoconvert !
"""
elif args.video_source == 'hdmi2usb':
# https://hdmi2usb.tv
# Note: this code works with 720p
video_src = """
v4l2src {attribs} name=videosrc !
queue !
image/jpeg,width=1280,height=720 !
jpegdec !
{monitor}
videoconvert !
videoscale !
videorate !
"""
elif args.video_source == 'ximage':
video_src = """
ximagesrc {attribs} name=videosrc
use-damage=false !
{monitor}
videoconvert !
videorate !
videoscale !
"""
# startx=0 starty=0 endx=1919 endy=1079 !
elif args.video_source == 'blackmagic':
video_src = """
decklinkvideosrc {attribs} !
{monitor}
videoconvert !
yadif !
videorate !
videoscale !
"""
elif args.video_source == 'png':
video_src = """
multifilesrc {attribs}
loop=1
caps="image/png" !
pngdec !
videoscale !
videoconvert !
"""
elif args.video_source == 'test':
d['hostname'] = socket.gethostname()
d['videocaps'] = videocaps
video_src = """
videotestsrc name=videosrc
{attribs} !
clockoverlay
text="Source:{hostname}\nCaps:{videocaps}\n"
halignment=left line-alignment=left !
{monitor}
"""
video_src = video_src.format( **d )
video_src += videocaps + "!\n"
return video_src
def mk_audio_src(args, audiocaps):
d={ 'attribs': args.audio_attribs }
if args.audio_source in [ 'dv', 'hdv' ]:
# this only works if video is from DV also.
# or some gst source that gets demux ed
audio_src = """
demux. !
audioconvert !
"""
elif args.audio_source == 'pulse':
audio_src = """
pulsesrc {attribs} name=audiosrc !
"""
elif args.audio_source == 'alsa':
audio_src = """
alsasrc {attribs} name=audiosrc !
"""
elif args.audio_source == 'blackmagic':
audio_src = """
decklinkaudiosrc {attribs} !
"""
elif args.audio_source == 'test':
audio_src = """
audiotestsrc {attribs} name=audiosrc freq=330 !
"""
audio_src = audio_src.format(**d)
audio_src += audiocaps + "!\n"
return audio_src
def mk_mux(args):
mux = """
mux.
matroskamux name=mux !
"""
return mux
def mk_client(args):
core_ip = socket.gethostbyname(args.host)
client = """
tcpclientsink host={host} port={port}
""".format(host=core_ip, port=args.port)
return client
def mk_pipeline(args, server_caps):
video_src = mk_video_src(args, server_caps['videocaps'])
audio_src = mk_audio_src(args, server_caps['audiocaps'])
mux = mk_mux(args)
client = mk_client(args)
pipeline = video_src + "mux.\n" + audio_src + mux + client
# remove blank lines to make it more human readable
while "\n\n" in pipeline:
pipeline = pipeline.replace("\n\n","\n")
return pipeline
def get_server_caps():
# fetch config from server
server_config = Connection.fetchServerConfig()
server_caps = {'videocaps': server_config['mix']['videocaps'],
'audiocaps': server_config['mix']['audiocaps']}
return server_caps
def run_pipeline(pipeline, args):
core_ip = socket.gethostbyname(args.host)
clock = GstNet.NetClientClock.new('voctocore', core_ip, 9998, 0)
print('obtained NetClientClock from host', clock)
print('waiting for NetClientClock to sync…')
clock.wait_for_sync(Gst.CLOCK_TIME_NONE)
print('starting pipeline')
senderPipeline = Gst.parse_launch(pipeline)
senderPipeline.use_clock(clock)
src = senderPipeline.get_by_name('src')
def on_eos(bus, message):
print('Received EOS-Signal')
sys.exit(1)
def on_error(bus, message):
# TypeError: on_error() missing 1 required positional argument: 'message'
print('Received Error-Signal')
(error, debug) = message.parse_error()
print('Error-Details: #%u: %s' % (error.code, debug))
sys.exit(1)
# Binding End-of-Stream-Signal on Source-Pipeline
senderPipeline.bus.add_signal_watch()
senderPipeline.bus.connect("message::eos", on_eos)
senderPipeline.bus.connect("message::error", on_error)
print("playing")
senderPipeline.set_state(Gst.State.PLAYING)
mainloop = GObject.MainLoop()
try:
mainloop.run()
except KeyboardInterrupt:
print('Terminated via Ctrl-C')
print('Shutting down...')
senderPipeline.set_state(Gst.State.NULL)
print('Done.')
return
def get_args():
parser = argparse.ArgumentParser(
description='''Vocto-ingest Client with Net-time support.
Gst caps are retrieved from the server.
Run without parameters: send test av to localhost:10000
''')
parser.add_argument('-v', '--verbose', action='count', default=0,
help="Also print INFO and DEBUG messages.")
parser.add_argument( '--video-source', action='store',
choices=[
'dv', 'hdv', 'hdmi2usb', 'blackmagic',
'ximage', 'png', 'test'],
default='test',
help="Where to get video from")
parser.add_argument( '--video-attribs', action='store',
default='',
help="misc video attributes for gst")
parser.add_argument( '--audio-source', action='store',
choices=['dv', 'alsa', 'pulse', 'blackmagic', 'test'],
default='test',
help="Where to get audio from")
parser.add_argument( '--audio-attribs', action='store',
default='',
help="misc audio attributes for gst")
parser.add_argument('-m', '--monitor', action='store_true',
help="fps display sink")
parser.add_argument( '--host', action='store',
default='localhost',
help="hostname of vocto core")
parser.add_argument( '--port', action='store',
default='10000',
help="port of vocto core")
parser.add_argument('--debug', action='store_true',
help="debugging things, like dump a gst-launch-1.0 command")
args = parser.parse_args()
return args
def main():
args = get_args()
core_ip = socket.gethostbyname(args.host)
# establish a synchronus connection to server
Connection.establish(core_ip)
server_caps = get_server_caps()
pipeline = mk_pipeline(args, server_caps)
print(pipeline)
if args.debug:
gst_cmd = "gst-launch-1.0 {}".format(pipeline)
# escape the ! because
# asl2: ! is interpreted as a command history metacharacter
gst_cmd = gst_cmd.replace("!"," \! ")
# remove all the \n to make it easy to cut/paste into shell
gst_cmd = gst_cmd.replace("\n"," ")
while " " in gst_cmd:
gst_cmd = gst_cmd.replace(" "," ")
print("-"*78)
print(gst_cmd)
print("-"*78)
run_pipeline(pipeline, args)
if __name__ == '__main__':
main()