2
# -*- coding: utf-8 -*-
4
# vim:si:ai:et:sw=4:sts=4:ts=4
8
# Copyright (c) 2011, Nicolas Bertrand <nico@inattendu.org>
10
# This program is free software; you can redistribute it and/or
11
# modify it under the terms of the GNU Lesser General Public
12
# License as published by the Free Software Foundation; either
13
# version 2.1 of the License, or (at your option) any later version.
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
# Lesser General Public License for more details.
20
# You should have received a copy of the GNU Lesser General Public
21
# License along with this program; if not, write to the
22
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23
# Boston, MA 02111-1307, USA.
27
# standard library imports
29
# related third party imports
38
# local application/library specific imports
39
import luciole.media.lgst.common as LG_COMMON
46
class ImageSinkBin(LG_COMMON.BaseBin) :
47
""" gst.Bin for Web cam input"""
49
"capture-done" : (gobject.SIGNAL_RUN_LAST,
51
(gobject.TYPE_PYOBJECT,)),
55
'name' : 'Ratefilter',
56
'element': 'capsfilter',
57
'media_type' : 'video/x-raw-rgb',
59
'framerate': '(fraction)1/1',
68
'element': 'fakesink',
70
'signal-handoffs': 'true',
76
'element': 'filesink',
78
'location': './test.jpeg',
83
# TODO : input should be x-raw-yuv
87
'element': 'videorate',
91
'element': 'ffmpegcolorspace',
94
'rate_filter' : RATE_FILTER,
97
'name' : 'ImgEncoder',
100
'imgsink' : IMG_SINK,
105
def __init__(self, filename, params = {} ) :
106
LG_COMMON.BaseBin.__init__(self, 'ImageSinkBin', params)
107
self.filename = filename
108
self.capture_handler_id = 0
113
LG_COMMON.make_element(self.params['rate'])
116
LG_COMMON.make_element(self.params['imgcsp'])
119
LG_COMMON.make_element(self.params['rate_filter'])
122
# LG_COMMON.make_element(self.params['imgenc'])
125
LG_COMMON.make_element(self.params['imgsink'])
137
# rate ! rate_filter ! imgsink
138
gst.element_link_many(
147
_str_link = ('%s ! %s ! %s' %\
149
self.rate_filter.bin_repr,
150
self.imgsink.bin_repr,
153
self.debug('%s : %s'%(self.get_name(), _str_link))
155
self.bin_repr = _str_link
158
self.make_ghostpads(sink = self.rate, src = None)
160
#_pad.set_blocked_async(True, self._have_data_cb)
163
def _have_data_cb(self, element, data) :
166
def capture(self, filename =None ) :
167
if self.capture_handler_id != 0 :
168
self.warning('Skipping capture request. Capture on progress')
171
#self.capture_handler_id = \
172
# self.imgenc.connect('frame-encoded', self.process_data_jpeg_cb, filename )
176
self.capture_handler_id = \
177
self.imgsink.connect('handoff', self.process_data_cb, filename )
180
def process_data_jpeg_cb(self, element, toto ) :
184
def process_data_cb(self, gbin ,data_buffer, pad, filename ) :
188
_caps = data_buffer.get_caps()
189
_structure = _caps[0]
190
_width = _structure["width"]
191
_height = _structure["height"]
193
self.imgsink.disconnect(self.capture_handler_id)
194
self.capture_handler_id = 0
198
_file = open(self.filename,'w')
199
_file.write(data_buffer)
207
_pixbuf = gtk.gdk.pixbuf_new_from_data(
208
data_buffer.copy_on_write(),
209
gtk.gdk.COLORSPACE_RGB,
214
data_buffer.size / _height)
217
# save pixbuf to file
218
if filename != None :
221
_filename = self.filename
222
_pixbuf.save(self.filename, "jpeg", {"quality":"100"})
224
self.debug('Write Jpeg on %s'%(_filename))
225
self.emit('capture-done', _filename)
229
gobject.type_register(ImageSinkBin)
230
gst.element_register(ImageSinkBin, 'image-sink')
233
if __name__ == '__main__':
234
from luciole.media.lgst.videotest_src import VideoTestSrc
235
from luciole.media.lgst.webcam_bin import WebcamBin
237
def _timeout_cb(gbin) :
241
def _capture_done_cb(gbin, param1) :
245
def make_sink_bin(filename) :
247
_rate = gst.element_factory_make('videorate')
249
_filter = gst.element_factory_make('capsfilter')
250
_caps = gst.Caps("video/x-raw-yuv, framerate=(fraction)1/1" )
251
_filter.set_property("caps", _caps)
253
_sink = gst.element_factory_make('fakesink')
254
#_sink.set_property('location', filename)
262
gst.element_link_many(
268
_sink_pad_bin = _rate
273
'sink', _sink_pad_bin.get_pad('sink')
280
pipeline = gst.Pipeline()
282
#_src = VideoTestSrc()
284
_totest = ImageSinkBin('./file.jpeg')
285
#_totest = make_sink_bin('./file.jpeg')
286
_totest.connect('capture-done', _capture_done_cb)
290
pipeline.add(_src, _totest)
293
pipeline.set_state(gst.STATE_PLAYING)
294
gobject.timeout_add_seconds(2, _timeout_cb, _totest)
295
gobject.MainLoop().run()