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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- Mode: Python -*-
# vim:si:ai:et:sw=4:sts=4:ts=4
import gst
from pitivi.log.loggable import Loggable
from pitivi.signalinterface import Signallable
from luciole.media.lgst.webcam_caps import WebcamCaps
class WebcamProps(Loggable, Signallable):
__signals__ = {
'caps-selected' : ['caps'],
}
BEST_RATIO = (720.0/576.0)
BEST_VIDEOTYPES = ['video/x-raw-yuv', 'video/x-raw-rgb']
BEST_YUV_FORMATS = [gst.Fourcc('I420'), gst.Fourcc('YUY2')]
VIDEOTYPE_PROPS = [
'red_mask',
'blue_mask',
'green_mask',
'bpp',
'depth',
'format'
]
FIELDS_TYPE = {
'width' : 'int' ,
'height' : 'int' ,
'framerate' : 'fraction' ,
'definition' : 'definition',
'videotype' : 'videotype',
'red_mask' : 'int' ,
'blue_mask' : 'int' ,
'green_mask' : 'int' ,
'bpp' :'int',
'depth' :'int',
'format': 'fourcc',
}
def __init__(self, widget) :
Loggable.__init__(self)
Signallable.__init__(self)
self.widget = widget
self._filter_caps = gst.caps_new_any()
self._webcam_caps = gst.caps_new_any()
self._selected_props = {}
self._selected_videotype = ""
self.PROPS_WITH_BEST_VALUE = {
'format' : self.get_best_yuv_format,
'definition' : self.get_best_definition,
'videotype' : self.get_best_videotype,
}
self._active_videotype_props = []
self._active_common_props = []
def prepare(self, caps) :
# store caps for webcam
self._webcam_caps = WebcamCaps(caps)
self.widget.connect('prop-changed', self._prop_changed_cb)
# first show definition
self.manage_prop('definition', None)
def get_best_value(self, prop, prop_values) :
_best = None
if prop in self.PROPS_WITH_BEST_VALUE :
_best = self.PROPS_WITH_BEST_VALUE[prop](prop_values)
else :
_best = prop_values[0]
self.info('No best value algorithm for %s. Fist value taken %s',
prop, _best)
return _best
def get_best_definition(self, definitions) :
""" algorithms who search first for best ratios,
then for higher width
"""
_def_dict = {}
# Write algotithm
_def_dict['width'] = definitions[-1]['width']
_def_dict['height'] = definitions[-1]['height']
return _def_dict
def get_best_videotype(self, videotypes) :
""" parse BEST_VIDEOTYPES list in preference order.
if videotype not in BEST_VIDEOTYPES return the first
videotype in input list
"""
_best_vtype = None
for _vtype in self.BEST_VIDEOTYPES :
if _vtype in videotypes :
_best_vtype = _vtype
break
if _best_vtype == None :
_best_vtype = videotypes[0]
self.warning( ("No best video type founds. selelcted %s."
"First in list") ) \
% _best_vtype
return _best_vtype
def get_best_yuv_format(self, yuv_formats) :
""" parse BEST_YUV_FORMAT list in preference order.
if videotype not in BEST_YUV_FORMAT return the first
videotype in input list
"""
_best = None
self.debug('avlb values : "%s', yuv_formats)
for _format in self.BEST_YUV_FORMATS :
if _format in yuv_formats :
_best = _format
break
if _best == None :
_best = yuv_formats[0]
print _best
self.warning("No best yuv format found. selelcted %s. First in list", _best)
self.debug("Best yuv fomat is %s",_best )
return _best
def get_best_framerate(self, framerates) :
""" take a framerate on the middle.
Not too high, not too low
"""
_len = len(framerates)
_best = _len/2
return _best
def _prop2str_single(self, name, value) :
_prop_str = ""
if self.FIELDS_TYPE[name] == 'fourcc' :
_prop_str = "%s" % value.fourcc
elif self.FIELDS_TYPE[name] == 'fraction' :
_prop_str = "%s/%s" % (value.num, value.denom)
elif self.FIELDS_TYPE[name] == 'definition' :
_prop_str = "%sx%s" % (value['width'], value['height'])
else :
_prop_str = "%s" % (value)
return _prop_str
def _prop2str(self, prop_name, values) :
_prop_out = None
if isinstance(values, list) :
_prop_out = [ self._prop2str_single(prop_name, _val) \
for _val in values ]
else :
_prop_out = self._prop2str_single(prop_name, values)
return _prop_out
return _prop_out
def _str2def(self, string) :
if string is not None :
_list = string.split('x')
_def_dict = {
'width' : _list[0],
'height' : _list[1]
}
return _def_dict
else :
self.error('Impossible to understand/interpret %s'%string)
return None
def set_prop_widget(self, prop, prop_values , active_value, wdg_type = 'combo' ) :
self.widget.set_prop(prop, prop_values , wdg_type )
# TODO : manage signal connect
self.debug("Active value for %s is %s", prop, active_value)
self.widget.set_active_prop(prop, active_value)
def set_framerates_widget(self, framerates, best_framerate ) :
pass
def _prop_changed_cb(self, widget, prop, value) :
if prop == 'definition' :
self._definition_changed_cb(value)
elif prop == 'videotype' :
self._videotype_changed_cb(value)
elif prop == 'framerate' :
self._framerate_changed_cb(value)
elif prop in self.VIDEOTYPE_PROPS :
self._videotype_prop_changed_cb(prop, value)
def _definition_changed_cb(self, str_definition) :
""" definition changed cb :
_ store definition
_ look videotype
"""
_selected_def = self._str2def(str_definition)
self._selected_props.update(_selected_def)
self._filter_caps = \
self._webcam_caps.get_caps_from_definition( _selected_def)
# when defintion changed clear videotype ( if exists)
self._clear_videotype()
self._clear_framerate()
#now look for videotypes
self.manage_prop('videotype', self._filter_caps)
def _videotype_changed_cb(self, videotype) :
self._selected_videotype = videotype
self.debug("RX videotype-changed : %s", self._selected_videotype)
_caps = self._make_caps(self._selected_videotype, self._selected_props)
self._filter_caps = self._webcam_caps.intersect(_caps)
_size = self._filter_caps.get_size()
self.debug(" %s selected caps :%s",
_size,
self._filter_caps ,
)
if _size == 0 :
self.error("No caps found for tpl %s ", _caps)
return
self._clear_videotype_props()
self._clear_framerate()
if _size == 1 :
# no more selection of props needed
# request for available framerates according definition
_is_single = self.manage_prop('framerate', self._filter_caps)
if _is_single == True :
self._check_single_caps(self._filter_caps)
else :
# more than one struct in caps
if self._selected_videotype == 'video/x-raw-yuv' :
self.manage_prop('format', self._filter_caps)
if self._selected_videotype == 'video/x-raw-rgb' :
self.manage_prop('red_mask', self._filter_caps)
def _videotype_prop_changed_cb(self, prop, value) :
self.debug("RX prop %s changed = %s", prop, value)
self._selected_props[prop] = value
self._clear_framerate()
_caps = self._make_caps(self._selected_videotype, self._selected_props)
self._filter_caps = self._webcam_caps.intersect(_caps)
_size = self._filter_caps.get_size()
self.debug(" %s selected caps :%s",
_size,
self._filter_caps ,
)
if _size == 0 :
self.error("No caps found for tpl %s ", _caps)
return
if _size > 1 :
self.warning(" Now what we do ???. What prop to select ? take the fitst_srtuct")
_single_caps = gst.Caps(self._filter_caps[0])
self._filter_caps = _single_caps
# no more selection of props needed
# request for available framerates according definition
_is_single = self.manage_prop('framerate', self._filter_caps)
if _is_single == True :
self._check_single_caps(self._filter_caps)
def _framerate_changed_cb(self, value) :
self.debug("RX framerate changed = %s (%s)", value, type(value))
if isinstance(value, float) :
value = gst.Fraction(value)
value = "%s/%s"%(int(value.num), int(value.denom))
print value
self._selected_props['framerate'] = value
_caps = self._make_caps(self._selected_videotype, self._selected_props)
self._filter_caps = self._webcam_caps.intersect(_caps)
self._filter_caps.get_size()
self.debug(" %s selected caps :%s",
self._filter_caps.get_size(),
self._filter_caps ,
)
# check if a single caps is set
self._check_single_caps(self._filter_caps)
def _check_single_caps(self, caps) :
if caps.get_size() == 1 \
and\
caps.is_fixed() == True :
self.debug("Hourra ! . caps %s can be applied to webcam", caps )
self.emit('caps-selected', caps)
def manage_prop(self, prop_name, caps) :
""" generic manage of a prop """
_is_single_value = False
is_range, _prop_values = \
self._webcam_caps.get_prop_values(prop_name , caps)
self.debug("%s = %s",prop_name, _prop_values)
if len(_prop_values) > 1 or is_range == True :
if is_range == False :
_best = self.get_best_value(prop_name, _prop_values)
_prop_values = self._prop2str(prop_name, _prop_values)
_best = self._prop2str(prop_name, _best)
self.set_prop_widget(prop_name, _prop_values , _best)
else :
_best = _prop_values[0]
self.set_prop_widget(prop_name, _prop_values , _best, 'range')
else :
self.info("Only one value %s for %s",_prop_values,prop_name )
_is_single_value = True
if prop_name in self.VIDEOTYPE_PROPS and _is_single_value == False :
self._active_videotype_props.append(prop_name)
return _is_single_value
def _clear_videotype(self) :
self._clear_videotype_props()
_prop = 'videotype'
if self._selected_videotype != "" :
self.widget.remove_prop(_prop)
self._selected_videotype = ""
def _clear_videotype_props(self) :
while self._active_videotype_props != [] :
_prop = self._active_videotype_props.pop()
self.widget.remove_prop(_prop)
del self._selected_props[_prop]
def _clear_framerate(self) :
_prop = 'framerate'
if _prop in self._selected_props :
self.widget.remove_prop(_prop)
del self._selected_props[_prop]
def clear_all(self) :
self.widget.remove_all_props()
self._selected_props.clear()
self._active_videotype_props = []
self._selected_videotype = ""
try :
self.widget.disconnect_by_function(self._prop_changed_cb)
except :
self.warning('Impossible to disconnect')
pass
def _make_caps(self, mediatype, fields) :
_caps_str = ""
for _key, _value in fields.iteritems() :
_caps_str = "%s, %s=(%s)%s" % ( \
_caps_str,
_key,
self.FIELDS_TYPE[_key],
_value
)
_caps_str = "%s %s" % (mediatype, _caps_str)
_caps = gst.Caps(_caps_str)
self.debug(" Caps created : %s", _caps.to_string())
return _caps
|