summaryrefslogtreecommitdiff
path: root/src/flufigut.py
blob: b68c856679f26fa63b2d8aa8ada6d64095605ccb (plain) (blame)
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
#!/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
        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 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_resampler(self, mux, format, profile, inputs, globals):
        source = mux['audio'].split(':')[0]
        input_samplerate = inputs[source]['properties']['samplerate']
        if 'samplerate' not in globals['formats'][format]:
            return None

        target_samplerate = globals['formats'][format]['samplerate']
        if target_samplerate == input_samplerate:
            return None

        feeder = 'input-%s' % (mux['audio'])
        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': feeder,
            'properties': {
                'samplerate': target_samplerate,
            },
        }
        return comp_name

    def __generate_video_resizer(self, mux, format, profile, inputs, globals):
        source = mux['video'].split(':')[0]
        input_resolution = inputs[source]['properties']['resolution']
        if 'video' not in globals['profiles'][profile]:
            return None

        if input_resolution == "":
            raise Exception("format definition needs video but no video input given")

        target_resolution = globals['profiles'][profile]['video']
        if target_resolution == input_resolution:
            return None

        if globals['resolutions'][target_resolution]['rate'] != globals['resolutions'][input_resolution]['rate']:
            raise Exception("ERROR: video rate conversion is not yet supported!!!")

        feeder = 'input-%s' % (mux['video'])
        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': feeder,
            'properties': {
                'width': globals['resolutions'][target_resolution]['width'],
                'height': globals['resolutions'][target_resolution]['height'],
            },
        }
        return comp_name

    def __generate_audio_encoder(self, mux, format, profile, inputs, globals, feeder):
        source = mux['audio'].split(':')[0]
        encoder = globals['formats'][format]['audio']
        bitrate = globals['profiles'][profile]['audio']
        samplerate = inputs[source]['properties']['samplerate']
        if 'samplerate' in globals['formats'][format]:
            samplerate = globals['formats'][format]['samplerate']

        if not feeder:
            feeder = 'input-%s' % (mux['audio'])

        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_encoder(self, mux, format, profile, inputs, globals, feeder):
        source = mux['video'].split(':')[0]
        encoder = globals['formats'][format]['video']
        resolution = globals['profiles'][profile]['video']
        bitrate = globals['bitrates'][encoder][resolution]

        if not feeder:
            feeder = 'input-%s' % (mux['video'])

        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:
                        resampler = self.__generate_audio_resampler(mux, format, profile, inputs, globals)
                        audio_encoder = self.__generate_audio_encoder(mux, format, profile, inputs, globals, resampler)
                    if 'video' in mux:
                        resizer = self.__generate_video_resizer(mux, format, profile, inputs, globals)
                        video_encoder = self.__generate_video_encoder(mux, format, profile, inputs, globals, resizer)

                    self.__generate_muxer(mux_name, format, profile, globals, audio_encoder, video_encoder)

    #
    # streams
    def _generate_streams(self, streams, globals):
        self.flow['repeaters'] = {}
        self.flow['streamers'] = {}
        pass

    #
    # 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.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)

    except Exception as e:
        print("ERROR: while running app: %s" % e)
        print(traceback.format_exc())
        sys.exit(1)

    sys.exit(ret)