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
|
#!/usr/bin/python
#
# flufigut
#
# flufigut, the flumotion configuration utility, is a simple tool
# that generates flumotion configuration files using pyhton jinja2
# template engine and simplejson. flufigut generates planet.xml
# and worker.xml files from configuration templates and an easy to
# understand representation of the flow structure written in json.
#
#
# Copyright (C) 2012 Christian Pointner <equinox@spreadspace.org>
# Michael Gebetsroither <michael@mgeb.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 simplejson as json
from exceptions import *
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))
### parse json file #############################################
#
if len(sys.argv) <= 2:
raise SystemExit("No template name and or configuration file given")
cf = open(sys.argv[2], 'r')
config = json.load(cf);
cf.close();
### initialization ##############################################
#
atmosphere = {}
flow = {}
machines = {}
globals = config['globals']
input = config['input']
transcode = config['transcode']
stream = config['stream']
### generate porter for all streamer/machines ###################
#
idx = 0
for streamer in stream:
worker = 'streamer%i'%(idx)
port = streamer['config']['port']
found = False
for machine in globals['machines'].keys():
if worker in globals['machines'][machine]:
if machine in machines:
if 'porter' in machines[machine]:
if port in machines[machine]['porter']:
print "Porter: machine %s already uses port %i" % (machine, port)
sys.exit(1)
else:
machines[machine]['porter'] = {}
else:
machines[machine] = { 'porter': {} }
found = True
machines[machine]['porter'][port] = {
'socket-path': "porter%i-%s"%(idx, rand_string()),
'username': rand_string(size=12),
'password': rand_string(size=12),
}
atmosphere['porter-%s-%i'%(machine, port)] = {
'type': "porter",
'desc': "Porter for %s on port %i"%(machine, port),
'worker': worker,
'properties': {
'port': port,
'socket-path': machines[machine]['porter'][port]['socket-path'],
'username': machines[machine]['porter'][port]['username'],
'password': machines[machine]['porter'][port]['password'],
},
}
if not found:
print "Streamer %i has no machine assigned" % (idx)
sys.exit(1)
idx+=1
### generate input components ###################################
flow['input'] = {}
flow['input']['raw-input'] = {
'type': input['source'],
'desc': "capture raw AV from %s" % input['source'],
'worker': 'input',
'properties': {},
}
for property in input.keys():
if property == 'samplerate':
flow['input']['raw-input']['properties'][property] = input['samplerate']
elif property == 'resolution':
flow['input']['raw-input']['properties']['width'] = globals['resolutions'][input[property]]['width']
flow['input']['raw-input']['properties']['height'] = globals['resolutions'][input[property]]['height']
flow['input']['raw-input']['properties']['framerate'] = globals['resolutions'][input[property]]['rate']
elif property != 'source':
flow['input']['raw-input']['properties'][property] = input[property]
resolutions = [ ]
for format in transcode.keys():
for profile in transcode[format]:
resolution = globals['profiles'][profile]['video']
if resolution not in resolutions:
if input['resolution'] != resolution:
if globals['resolutions'][resolution]['rate'] != globals['resolutions'][input['resolution']]['rate']:
print "video rate conversion is not yet supported!!!"
sys.exit(-1)
resolutions.append(resolution)
for resolution in resolutions:
flow['input']['resize-%s' % resolution] = {
'type': 'video-resize',
'desc': "resize video to %sx%s" % (globals['resolutions'][resolution]['width'], globals['resolutions'][resolution]['height']),
'worker': 'input',
'feeder': 'raw-input:video',
'properties': {
'width': globals['resolutions'][resolution]['width'],
'height': globals['resolutions'][resolution]['height'],
},
}
samplerates = [ ]
for format in globals['formats'].keys():
if 'samplerate' in globals['formats'][format]:
samplerate = globals['formats'][format]['samplerate']
if samplerate not in samplerates:
if input['samplerate'] != samplerate:
samplerates.append(samplerate)
for samplerate in samplerates:
flow['input']['resample-%s' % samplerate] = {
'type': 'audio-resample',
'desc': "resample audio to %s Hz" % samplerate,
'worker': 'input',
'feeder': 'raw-input:audio',
'properties': {
'samplerate': samplerate,
},
}
### generate encoder and muxer components #######################
flow['encoder_video'] = {}
flow['encoder_audio'] = {}
flow['muxer'] = {}
for format in transcode.keys():
for profile in transcode[format]:
video_encoder = 'none'
if 'video' in globals['formats'][format]:
encoder = globals['formats'][format]['video']
resolution = globals['profiles'][profile]['video']
bitrate = globals['bitrates'][encoder][resolution]
if resolution == input['resolution']:
feeder = 'raw-input:video'
else:
feeder = 'resize-%s' % resolution
video_encoder = 'encode-%s-%s' % (encoder, resolution)
flow['encoder_video'][video_encoder] = {
'type': '%s-encode' % encoder,
'desc': "%s encoder for %sx%s" % (encoder, globals['resolutions'][resolution]['width'], globals['resolutions'][resolution]['height']),
'worker': 'encoder-%s' % encoder,
'feeder': feeder,
'properties': {
'bitrate': bitrate,
},
}
audio_encoder = 'none'
if 'audio' in globals['formats'][format]:
encoder = globals['formats'][format]['audio']
bitrate = globals['profiles'][profile]['audio']
if 'samplerate' in globals['formats'][format]:
samplerate = globals['formats'][format]['samplerate']
feeder = 'resample-%s' % samplerate
else:
samplerate = input['samplerate']
feeder = 'raw-input:audio'
audio_encoder = 'encode-%s-%i-%i' % (encoder, bitrate, samplerate)
flow['encoder_audio'][audio_encoder] = {
'type': '%s-encode' % encoder,
'desc': "%s encoder for %i kbit/s @ %i Hz" % (encoder, bitrate, samplerate),
'worker': 'encoder-%s' % encoder,
'feeder': feeder,
'properties': {
'bitrate': bitrate,
},
}
muxer = globals['formats'][format]['muxer']
flow['muxer']['muxer-%s-%s' % (muxer, profile)] = {
'type': '%s-mux' % muxer,
'desc': "%s muxer profile %s" % (format, profile),
'worker': 'muxer-%s' % muxer,
'feeder_audio': audio_encoder,
'feeder_video': video_encoder,
'properties': {},
}
### generate streamer components ################################
flow['streamer'] = {}
# TODO
### initialize and render templates #############################
#
env = Environment(loader=FileSystemLoader('../templates/%s/' % (sys.argv[1])), line_statement_prefix = '%%')
template = env.get_template('planet.xml')
planet = template.render(globals=globals, atmosphere=atmosphere, flow=flow)
sys.stdout.write(planet.encode("utf8"))
### end #########################################################
|