-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture.py
230 lines (189 loc) · 7.94 KB
/
capture.py
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
# -*- coding: UTF-8 -*-
# Copyright (c) 2012, Miguel Paolino <[email protected]>
# This file is part of Cuadraditos.
#
# Cuadraditos 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 3 of the License, or
# (at your option) any later version.
#
# Cuadraditos 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 Cuadraditos. If not, see <http://www.gnu.org/licenses/>.
import gtk
import pygtk
pygtk.require('2.0')
import pygst
pygst.require('0.10')
import gst
import gst.interfaces
import gobject
gobject.threads_init()
import logging
from constants import Constants
import aplay
import urlparse
from sugar.graphics.alert import ConfirmationAlert, TimeoutAlert
logger = logging.getLogger('cuadraditos:capture.py')
def _is_camera_present():
v4l2src = gst.element_factory_make('v4l2src')
if v4l2src.props.device_name is None:
return False, False
# Figure out if we can place a framerate limit on the v4l2 element, which
# in theory will make it all the way down to the hardware.
# ideally, we should be able to do this by checking caps. However, I can't
# find a way to do this (at this time, XO-1 cafe camera driver doesn't
# support framerate changes, but gstreamer caps suggest otherwise)
pipeline = gst.Pipeline()
caps = gst.Caps("video/x-raw-yuv")
fsink = gst.element_factory_make("fakesink")
pipeline.add(v4l2src, fsink)
v4l2src.link(fsink, caps)
pipeline.set_state(gst.STATE_NULL)
return True
camera_presents = _is_camera_present()
class Capture:
log = logging.getLogger('cuadraditos-capture')
def __init__(self, pca):
self.window = None
self.ca = pca
self._eos_cb = None
self.playing = False
self.VIDEO_WIDTH = 352
self.VIDEO_HEIGHT = 288
self.VIDEO_FRAMERATE = 5
self.recognized_schemes = ['file', 'ftp', 'gopher', 'http', 'https',
'imap', 'mailto', 'mms', 'news', 'nntp',
'prospero', 'rsync', 'rtsp', 'rtspu',
'sftp', 'shttp', 'sip', 'sips', 'snews',
'svn', 'svn+ssh', 'telnet', 'wais']
self.pipeline = gst.Pipeline("my-pipeline")
self.createPipeline()
self.last_detection = None
bus = self.pipeline.get_bus()
bus.enable_sync_message_emission()
bus.add_signal_watch()
self.SYNC_ID = bus.connect('sync-message::element',\
self._onSyncMessageCb)
self.MESSAGE_ID = bus.connect('message', self._onMessageCb)
def createPipeline(self):
src = gst.element_factory_make("v4l2src", "camsrc")
srccaps = gst.Caps('video/x-raw-yuv, width=%s, height=%s' %\
(self.VIDEO_WIDTH, self.VIDEO_HEIGHT))
zbar = gst.element_factory_make("zbar")
self.pipeline.add(src, zbar)
src.link(zbar, srccaps)
xvsink = gst.element_factory_make("xvimagesink", "xvsink")
xv_available = xvsink.set_state(gst.STATE_PAUSED) != \
gst.STATE_CHANGE_FAILURE
xvsink.set_state(gst.STATE_NULL)
if not xv_available:
self.__class__.log.error('xv not available cannot capture video')
return
xvsink.set_property("sync", False)
self.pipeline.add(xvsink)
zbar.link(xvsink)
def play(self):
if not camera_presents:
return
self.pipeline.set_state(gst.STATE_PLAYING)
self.playing = True
def pause(self):
self.pipeline.set_state(gst.STATE_PAUSED)
self.playing = False
def stop(self):
self.pipeline.set_state(gst.STATE_NULL)
self.playing = False
def is_playing(self):
return self.playing
def idlePlayElement(self, element):
element.set_state(gst.STATE_PLAYING)
return False
def blockedCb(self, x, y, z):
pass
def _onSyncMessageCb(self, bus, message):
if message.structure is None:
return
if message.structure.get_name() == 'prepare-xwindow-id':
self.window.set_sink(message.src)
message.src.set_property('force-aspect-ratio', True)
def _clipboardGetFuncCb(self, clipboard, selection_data, info, data):
selection_data.set("text/uri-list", 8, data)
def _clipboardClearFuncCb(self, clipboard, data):
pass
def _copyURIToClipboard(self):
gtk.Clipboard().set_with_data([('text/uri-list', 0, 0)],
self._clipboardGetFuncCb,
self._clipboardClearFuncCb,
self.last_detection)
return True
def _alert_uri_response_cb(self, alert, response_id):
self.ca.remove_alert(alert)
def _alert_text_response_cb(self, alert, response_id):
if response_id is gtk.RESPONSE_OK:
gtk.Clipboard().set_text(self.last_detection)
self.ca.remove_alert(alert)
def _onMessageCb(self, bus, message):
t = message.type
if t == gst.MESSAGE_EOS:
if self._eos_cb:
cb = self._eos_cb
self._eos_cb = None
cb()
elif t == gst.MESSAGE_ELEMENT:
s = message.structure
if s.has_name("barcode"):
self.stop()
self.window.hide()
aplay.play(Constants.sound_click)
self.last_detection = s['symbol']
parsedurl = urlparse.urlparse(s['symbol'])
if parsedurl.scheme in self.recognized_schemes:
alert = TimeoutAlert(60)
alert.remove_button(gtk.RESPONSE_CANCEL)
alert.props.title = 'Direccion detectada!'
alert.props.msg = 'La dirección fue copiada al ' +\
'portatapeles. Acceda al ' +\
'marco de Sugar y haga click sobre ' +\
'ella para abrirla en el navegador.'
alert.connect('response', self._alert_uri_response_cb)
self.ca.add_alert(alert)
self._copyURIToClipboard()
self.ca.alert.show()
else:
alert = ConfirmationAlert()
alert.props.title = 'Texto detectado. ' +\
'¿Desea copiarlo al portapapeles?'
alert.props.msg = s['symbol']
alert.connect('response', self._alert_text_response_cb)
self.ca.add_alert(alert)
self.ca.alert.show()
elif t == gst.MESSAGE_ERROR:
#todo: if we come out of suspend/resume with errors, then get us
# back up and running...
#todo: handle "No space left on the resource.gstfilesink.c"
#err, debug = message.parse_error()
pass
class LiveCaptureWindow(gtk.Window):
def __init__(self, bgd):
gtk.Window.__init__(self)
self.imagesink = None
self.capture = None
self.modify_bg(gtk.STATE_NORMAL, bgd)
self.modify_bg(gtk.STATE_INSENSITIVE, bgd)
self.unset_flags(gtk.DOUBLE_BUFFERED)
self.set_flags(gtk.APP_PAINTABLE)
def set_capture(self, pcapture):
self.capture = pcapture
self.capture.window = self
def set_sink(self, sink):
if (self.imagesink != None):
assert self.window.xid
self.imagesink = None
del self.imagesink
self.imagesink = sink
self.imagesink.set_xwindow_id(self.window.xid)