-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintercept-vstream.py
188 lines (163 loc) · 5.37 KB
/
intercept-vstream.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
#!/usr/bin/env python
import sys, os
import pygtk, gtk, gobject
import pygst
pygst.require("0.10")
import gst
import md5
import traceback
import cairo
from math import pi
class NewElement(gst.Element):
""" A basic, buffer forwarding gstreamer element """
#here we register our plugin details
__gstdetails__ = (
"NewElement plugin",
"newelement.py",
"gst.Element, that passes a buffer from source to sink (a filter)",
"Stephen Griffiths <[email protected]>")
#source pad (template): we send buffers forward through here
_srctemplate = gst.PadTemplate ('src',
gst.PAD_SRC,
gst.PAD_ALWAYS,
gst.caps_new_any())
#sink pad (template): we recieve buffers from our sink pad
_sinktemplate = gst.PadTemplate ('sink',
gst.PAD_SINK,
gst.PAD_ALWAYS,
gst.caps_new_any())
#register our pad templates
__gsttemplates__ = (_srctemplate, _sinktemplate)
def __init__(self, *args, **kwargs):
#initialise parent class
gst.Element.__init__(self, *args, **kwargs)
#source pad, outgoing data
self.srcpad = gst.Pad(self._srctemplate)
#sink pad, incoming data
self.sinkpad = gst.Pad(self._sinktemplate)
self.sinkpad.set_setcaps_function(self._sink_setcaps)
self.sinkpad.set_chain_function(self._sink_chain)
#make pads available
self.add_pad(self.srcpad)
self.add_pad(self.sinkpad)
def _sink_setcaps(self, pad, caps):
#we negotiate our capabilities here, this function is called
#as autovideosink accepts anything, we just say yes we can handle the
#incoming data
return True
def _sink_chain(self, pad, buf):
print "filter", len(buf), md5.new(buf).hexdigest()
#this is where we do filtering
#and then push a buffer to the next element, returning a value saying
# it was either successful or not.
outbuf = buf.copy_on_write()
self.draw_on(outbuf)
return self.srcpad.push(outbuf)
o=0
def draw_on (self, buf):
try:
caps = buf.get_caps()
width = caps[0]['width']
height = caps[0]['height']
framerate = caps[0]['framerate']
# width = width/2
height = height/2
surface = cairo.ImageSurface.create_for_data (buf, cairo.FORMAT_ARGB32, width, height, 4 * width)
ctx = cairo.Context(surface)
except:
print "Failed to create cairo surface for buffer"
traceback.print_exc()
return
try:
center_x = (width/4 + self.o)%width
center_y = (3*height/4 + self.o)%height
self.o+=10
# draw a circle
radius = float (min (width, height)) * 0.25
ctx.set_source_rgba (0.0, 0.0, 0.0, 0.9)
ctx.move_to (center_x, center_y)
ctx.arc (center_x, center_y, radius, 0, 2.0*pi)
ctx.close_path()
ctx.fill()
ctx.set_source_rgba (1.0, 1.0, 1.0, 1.0)
ctx.set_font_size(0.3 * radius)
txt = "Hello World"
extents = ctx.text_extents (txt)
ctx.move_to(center_x - extents[2]/2, center_y + extents[3]/2)
ctx.text_path(txt)
ctx.fill()
except:
print "Failed cairo render"
traceback.print_exc()
#here we register our class with glib, the c-based object system used by
#gstreamer
gobject.type_register(NewElement)
class GTK_Main:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("Intercepting web-cam")
window.set_default_size(500, 400)
window.connect("destroy", gtk.main_quit, "WM destroy")
vbox = gtk.VBox()
window.add(vbox)
self.movie_window = gtk.DrawingArea()
vbox.add(self.movie_window)
hbox = gtk.HBox()
vbox.pack_start(hbox, False)
hbox.set_border_width(10)
hbox.pack_start(gtk.Label())
self.button = gtk.Button("Start")
self.button.connect("clicked", self.start_stop)
hbox.pack_start(self.button, False)
self.button2 = gtk.Button("Quit")
self.button2.connect("clicked", self.exit)
hbox.pack_start(self.button2, False)
hbox.add(gtk.Label())
window.show_all()
# Set up the gstreamer pipeline
self.player = gst.Pipeline("player")
src = gst.element_factory_make ("v4l2src")
cf = gst.element_factory_make ("capsfilter")
WIDTH, HEIGHT, FRAMERATE = 640, 480, 15
caps = gst.caps_from_string ("video/x-raw-yuv,format=(fourcc)YUY2,width=%d,height=%d,framerate=%d/1" % (WIDTH, HEIGHT, FRAMERATE))
cf.set_property ("caps", caps)
filter = NewElement()
sink = gst.element_factory_make('autovideosink')
self.player.add(src, cf, filter, sink)
gst.element_link_many(src, cf, filter, sink)
bus = self.player.get_bus()
bus.add_signal_watch()
bus.enable_sync_message_emission()
bus.connect("message", self.on_message)
bus.connect("sync-message::element", self.on_sync_message)
def start_stop(self, w):
if self.button.get_label() == "Start":
self.button.set_label("Stop")
self.player.set_state(gst.STATE_PLAYING)
else:
self.player.set_state(gst.STATE_NULL)
self.button.set_label("Start")
def exit(self, widget, data=None):
gtk.main_quit()
def on_message(self, bus, message):
t = message.type
if t == gst.MESSAGE_EOS:
self.player.set_state(gst.STATE_NULL)
self.button.set_label("Start")
elif t == gst.MESSAGE_ERROR:
err, debug = message.parse_error()
print "Error: %s" % err, debug
self.player.set_state(gst.STATE_NULL)
self.button.set_label("Start")
def on_sync_message(self, bus, message):
if message.structure is None:
return
message_name = message.structure.get_name()
if message_name == "prepare-xwindow-id":
# Assign the viewport
imagesink = message.src
imagesink.set_property("force-aspect-ratio", True)
imagesink.set_xwindow_id(self.movie_window.window.xid)
GTK_Main()
gtk.gdk.threads_init()
gtk.main()