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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
#!/usr/bin/python3
#
# flufigut
#
# flufigut, the flumotion configuration utility, is a simple tool
# that generates flumotion configuration files using pyhton jinja2
# template engine. flufigut generates planet.xml and worker.xml
# files from configuration templates and an easy to understand
# representation of the flow structure written in json or yaml.
#
#
# Copyright (C) 2018 Christian Pointner <equinox@spreadspace.org>
#
# This file is part of flufigut.
#
# flufigut is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# any later version.
#
# flufigut is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with flufigut. If not, see <http://www.gnu.org/licenses/>.
#
import string
import random
import sys
import yaml
# from jinja2 import Environment, FileSystemLoader
# helper functions ############################################
#
def rand_string(size=8, chars=string.ascii_lowercase + string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))
# a flufigut stream description ###############################
#
class Description:
def __init__(self):
self.globals = {}
self.inputs = {}
self.muxes = {}
self.streams = {}
self.records = {}
def _sanity_check(self):
# TODO: add more sanity checks
# - check if stream['localdup']['port'] and stream['port'] are different
components = {}
for _, worker in self.globals['workers'].items():
for c in worker:
if c in components:
raise Exception("ERROR: component '%s' is assigned to more than one worker!" % c)
else:
components[c] = 1
def parse(self, config_file):
cf = open(config_file, 'r')
config = yaml.load(cf)
cf.close()
self.globals = config['globals']
self.inputs = config['inputs']
self.muxes = config['muxes']
self.streams = config['streams']
if 'records' in config:
self.records = config['records']
return self._sanity_check()
# a flumtion planet configuration #############################
#
class Porter:
def __init__(self, name, interface, port):
self.name = name
self.interface = interface
self.port = port
self.socket_path = rand_string()
self.username = rand_string(size=12)
self.password = rand_string(size=20)
class Planet:
def __init__(self):
self.atmosphere = {}
self.flow = {}
#
# inputs
def __set_input_properties(self, comp_name, props, globals):
for prop in props:
if prop == 'resolution':
self.flow['inputs'][comp_name]['properties']['width'] = globals['resolutions'][props[prop]]['width']
self.flow['inputs'][comp_name]['properties']['height'] = globals['resolutions'][props[prop]]['height']
self.flow['inputs'][comp_name]['properties']['framerate'] = globals['resolutions'][props[prop]]['rate']
else:
self.flow['inputs'][comp_name]['properties'][prop] = props[prop]
def _generate_inputs(self, inputs, globals):
self.flow['inputs'] = {}
master_cnt = 0
for source, input in inputs.items():
comp_name = 'input-%s' % source
comp_desc = 'capture raw data from %s' % (source)
self.flow['inputs'][comp_name] = {
'type': input['type'],
'desc': comp_desc,
'worker': None,
'master': input['master'],
'properties': {},
}
if input['master']:
master_cnt += 1
self.__set_input_properties(comp_name, input['properties'], globals)
if master_cnt == 0:
raise Exception("You have not configured any master clock device!")
elif master_cnt > 1:
raise Exception("You have configured multiple master clock devices!")
#
# muxes
def __generate_audio_feeder(self, input, source, input_samplerate, target_samplerate):
if target_samplerate == input_samplerate:
return input
comp_name = 'resample-%s-%s' % (source, target_samplerate)
comp_desc = 'resample audio from % s to % s Hz' % (source, target_samplerate)
self.flow['inputs'][comp_name] = {
'type': 'audio-resample',
'desc': comp_desc,
'worker': None,
'feeder': input,
'properties': {
'samplerate': target_samplerate,
},
}
return comp_name
def __generate_audio_encoder(self, mux, format, profile, inputs, globals):
source = mux['audio'].split(':')[0]
encoder = globals['formats'][format]['audio']
bitrate = globals['profiles'][profile]['audio']
input_samplerate = inputs[source]['properties']['samplerate']
samplerate = input_samplerate
if 'samplerate' in globals['formats'][format]:
samplerate = globals['formats'][format]['samplerate']
feeder = self.__generate_audio_feeder('input-%s' % (mux['audio']), source, input_samplerate, samplerate)
comp_name = 'encode-%s-%s-%i-%i' % (source, encoder, bitrate, samplerate)
comp_desc = '%s encoder for %i kbit/s @ %i Hz, from %s' % (encoder, bitrate, samplerate, source),
if bitrate == 0:
comp_name = 'encode-%s-%s-%i' % (source, encoder, samplerate)
comp_desc = '%s encoder @ %i Hz, from %s' % (encoder, samplerate, source),
if comp_name in self.flow['encoders-audio']:
return comp_name
self.flow['encoders-audio'][comp_name] = {
'type': '%s-encode' % encoder,
'desc': comp_desc,
'worker': None,
'feeder': feeder,
'properties': {
'bitrate': bitrate,
},
}
return comp_name
def __generate_video_feeder(self, input, source, input_resolution, target_resolution, globals):
if globals['resolutions'][target_resolution]['rate'] != globals['resolutions'][input_resolution]['rate']:
raise Exception("ERROR: video rate conversion is not supported!!!")
if target_resolution == input_resolution:
return input
comp_name = 'resize-%s-%s' % (source, target_resolution)
comp_desc = 'resize video from %s to %sx%s' % (source, globals['resolutions'][target_resolution]['width'],
globals['resolutions'][target_resolution]['height']),
self.flow['inputs'][comp_name] = {
'type': 'video-resize',
'desc': comp_desc,
'worker': None,
'feeder': input,
'properties': {
'width': globals['resolutions'][target_resolution]['width'],
'height': globals['resolutions'][target_resolution]['height'],
},
}
return comp_name
def __generate_video_encoder(self, mux, format, profile, inputs, globals):
if 'video' not in globals['profiles'][profile]:
return None
source = mux['video'].split(':')[0]
encoder = globals['formats'][format]['video']
input_resolution = inputs[source]['properties']['resolution']
resolution = globals['profiles'][profile]['video']
bitrate = globals['bitrates'][encoder][resolution]
if input_resolution == "":
raise Exception("format definition needs video but no video input given")
feeder = self.__generate_video_feeder('input-%s' % (mux['video']), source, input_resolution, resolution, globals)
comp_name = 'encode-%s-%s-%s' % (source, encoder, resolution)
comp_desc = '%s encoder for %sx%s, from %s' % (encoder, globals['resolutions'][resolution]['width'],
globals['resolutions'][resolution]['height'], source),
if comp_name in self.flow['encoders-video']:
return comp_name
self.flow['encoders-video'][comp_name] = {
'type': '%s-encode' % encoder,
'desc': comp_desc,
'worker': None,
'feeder': feeder,
'properties': {
'bitrate': bitrate,
},
}
return comp_name
def __generate_muxer(self, mux_name, format, profile, globals, feeder_audio, feeder_video):
muxer = globals['formats'][format]['muxer']
comp_name = 'mux-%s-%s-%s' % (mux_name, format, profile)
comp_desc = '%s muxer for %s, profile %s' % (format, mux_name, profile),
self.flow['muxers'][comp_name] = {
'type': '%s-mux' % muxer,
'desc': comp_desc,
'worker': None,
'feeder_audio': feeder_audio,
'feeder_video': feeder_video,
'properties': {},
}
def _generate_muxes(self, muxes, inputs, globals):
self.flow['encoders-audio'] = {}
self.flow['encoders-video'] = {}
self.flow['muxers'] = {}
for mux_name, mux in muxes.items():
for format in mux['formats']:
for profile in mux['formats'][format]:
audio_encoder = None
video_encoder = None
if 'audio' in mux:
audio_encoder = self.__generate_audio_encoder(mux, format, profile, inputs, globals)
if 'video' in mux:
video_encoder = self.__generate_video_encoder(mux, format, profile, inputs, globals)
self.__generate_muxer(mux_name, format, profile, globals, audio_encoder, video_encoder)
#
# streams
def __create_porter(self, stream, idx, port, interface=None):
comp_name = 'port-%s%i-%i' % (stream, idx + 1, port)
addr = '*:%i' % (port)
if interface:
comp_name = 'port-%s%i-%s-%i' % (stream, idx + 1, interface, port)
addr = '%s:%i' % (interface, port)
porter = Porter(comp_name, interface, port)
self.atmosphere[porter.name] = {
'type': "porter",
'desc': "Porter for %s%i on %s" % (stream, idx + 1, addr),
'worker': None,
'properties': {
'port': port,
'socket-path': porter.socket_path,
'username': porter.username,
'password': porter.password,
},
}
if interface:
self.atmosphere[porter.name]['interface'] = interface
return porter
def __generate_stream_mux_repeater(self, stream, mux, format, profile, feeder):
comp_name = 'repeater-%s-%s-%s-%s' % (stream, mux, format, profile)
if comp_name in self.flow['repeaters']:
return comp_name
self.flow['repeaters'][comp_name] = {
'type': 'repeater',
'desc': "repeater for %s (%s %s-%s)" % (stream, mux, format, profile),
'worker': None,
'feeder': feeder,
}
return comp_name
def __generate_stream_mux_instance(self, stream_name, stream, mux, format, profile, idx, cnt, porter, globals):
muxer_feed = 'muxer-%s-%s-%s' % (mux, format, profile)
feeder = muxer_feed
if 'repeater' in stream:
feeder = self.__generate_stream_mux_repeater(stream_name, mux, format, profile, muxer_feed)
comp_name = '%s-%s%i-%s-%s-%s' % (stream['type'], stream_name, idx + 1, mux, format, profile)
mount_point = '/%s-%s-%s.%s' % (mux, format, profile, globals['formats'][format]['muxer'])
if cnt > 1:
hostname = stream['hostname'] % (idx + 1)
if idx != 0:
hostname_next = stream['hostname'] % (idx)
else:
hostname_next = stream['hostname'] % (cnt)
else:
hostname = stream['hostname']
self.flow['streamers'][comp_name] = {
'type': "%s-stream" % stream['type'],
'desc': "%s streamer for %s %s-%s (part %i of %i in %s cluster)" % (stream['type'], mux, format, profile, idx + 1, cnt, stream_name),
'worker': None,
'feeder': feeder,
'properties': {
'description': globals['description'],
'type': 'slave',
'porter-socket-path': porter.socket_path,
'porter-username': porter.username,
'porter-password': porter.password,
'mount-point': mount_point,
'hostname': hostname,
'port': porter.port,
}
}
# TODO: add support for max-con, max-bw and burst-on-connect!
# TODO: add stats!!
def __generate_stream_instance(self, stream_name, stream, idx, cnt, muxes, globals):
port = stream['port']
interface = None
if 'interface' in stream:
interface = stream['interface']
porter = self.__create_porter(stream_name, idx, port, interface)
for mux_name in stream['muxes']:
for format_name, format in muxes[mux_name]['formats'].items():
for profile_name in format:
self.__generate_stream_mux_instance(stream_name, stream, mux_name, format_name, profile_name,
idx, cnt, porter, globals)
def _generate_streams(self, streams, muxes, globals):
self.flow['repeaters'] = {}
self.flow['streamers'] = {}
for stream_name, stream in streams.items():
cnt = stream['count']
for idx in range(cnt):
self.__generate_stream_instance(stream_name, stream, idx, cnt, muxes, globals)
#
# records
def _generate_records(self, records, globals):
self.flow['recorders'] = {}
for _, record in records.items():
for mux in record['muxes']:
format = record['muxes'][mux]["format"]
profile = record['muxes'][mux]["profile"]
feeder = 'mux-%s-%s-%s' % (mux, format, profile)
comp_name = 'record-%s-%s-%s' % (mux, format, profile)
comp_desc = 'recorder for %s %s-%s' % (mux, format, profile),
self.flow['recorders'][comp_name] = {
'type': "recorder",
'desc': comp_desc,
'worker': None,
'feeder': feeder,
'properties': {}
}
for prop in record:
if prop != 'muxes':
self.flow['recorders'][comp_name]['properties'][prop] = record[prop]
#
# all
def generate(self, desc):
self._generate_inputs(desc.inputs, desc.globals)
self._generate_muxes(desc.muxes, desc.inputs, desc.globals)
self._generate_streams(desc.streams, desc.muxes, desc.globals)
self._generate_records(desc.records, desc.globals)
# Main ########################################################
#
if __name__ == '__main__':
import traceback
import pprint
__pp = pprint.PrettyPrinter(indent=4, width=160)
if len(sys.argv) <= 1:
print("ERROR: No configuration file given")
sys.exit(-1)
config_file = sys.argv[1]
ret = 0
try:
d = Description()
d.parse(config_file)
p = Planet()
p.generate(d)
print("****************************************************")
print("** atmosphere **")
print("**")
__pp.pprint(p.atmosphere)
print("**")
print("**************************")
print("** planet **")
print("**")
__pp.pprint(p.flow)
print("**")
print("****************************************************")
except Exception as e:
print("ERROR: while running app: %s" % e)
print(traceback.format_exc())
sys.exit(1)
sys.exit(ret)
|