1 | #!/usr/bin/python |
---|
2 | import logging |
---|
3 | |
---|
4 | from gi.repository import Gst |
---|
5 | from gi.repository import Gtk |
---|
6 | from gi.repository import GLib |
---|
7 | from gi.repository import GObject |
---|
8 | |
---|
9 | # GstVideo and GdkX11 are needed to assign the xid and show the viewfinder |
---|
10 | # in the drawingarea |
---|
11 | from gi.repository import GstVideo |
---|
12 | from gi.repository import GdkX11 |
---|
13 | |
---|
14 | GObject.threads_init() |
---|
15 | Gst.init(None) |
---|
16 | logging.error('Using GStreamer version %s', Gst.version_string()) |
---|
17 | |
---|
18 | class Main: |
---|
19 | def __init__(self): |
---|
20 | |
---|
21 | win = Gtk.Window() |
---|
22 | self.draw = Gtk.DrawingArea() |
---|
23 | self.draw.set_app_paintable(True) |
---|
24 | self.draw.set_double_buffered(False) |
---|
25 | vbox = Gtk.VBox() |
---|
26 | buttons_box = Gtk.HBox() |
---|
27 | take_photo_button = Gtk.Button('Take photo') |
---|
28 | take_photo_button.connect('clicked', self.__take_photo_cb) |
---|
29 | buttons_box.add(take_photo_button) |
---|
30 | record_button = Gtk.Button('Record video') |
---|
31 | record_button.connect('clicked', self.__start_recording_cb) |
---|
32 | buttons_box.add(record_button) |
---|
33 | stop_button = Gtk.Button('Stop video') |
---|
34 | stop_button.connect('clicked', self.__stop_recording_cb) |
---|
35 | buttons_box.add(stop_button) |
---|
36 | |
---|
37 | vbox.pack_start(self.draw, True, True, 0) |
---|
38 | vbox.pack_start(buttons_box, False, False, 0) |
---|
39 | win.add(vbox) |
---|
40 | win.set_default_size(600, 400) |
---|
41 | win.show_all() |
---|
42 | win.connect("destroy", Gtk.main_quit) |
---|
43 | self._drawarea_xid = self.draw.get_window().get_xid() |
---|
44 | |
---|
45 | self.pipeline = Gst.Pipeline() |
---|
46 | self.bus = self.pipeline.get_bus() |
---|
47 | self.bus.add_signal_watch() |
---|
48 | # This is needed to make the video output in our DrawingArea |
---|
49 | self.bus.enable_sync_message_emission() |
---|
50 | self.bus.connect('sync-message::element', self.__on_sync_message) |
---|
51 | self.camerabin = Gst.ElementFactory.make('camerabin', 'camera') |
---|
52 | if self.camerabin is None: |
---|
53 | logging.error('Can\'t initialize camerabin.' |
---|
54 | 'Check if the plugin is installed') |
---|
55 | self.pipeline.add(self.camerabin) |
---|
56 | self.pipeline.set_state(Gst.State.PLAYING) |
---|
57 | |
---|
58 | def __on_sync_message(self, bus, msg): |
---|
59 | if msg.get_structure().get_name() == 'prepare-window-handle': |
---|
60 | msg.src.set_window_handle(self._drawarea_xid) |
---|
61 | if msg.get_structure().get_name() == 'image-done': |
---|
62 | logging.error('IMAGE DONE!!!') |
---|
63 | |
---|
64 | def __take_photo_cb(self, button): |
---|
65 | self.camerabin.set_property("mode", 1) # 1: Photo, 2: Video |
---|
66 | self.camerabin.set_property("location", "foo.jpg") |
---|
67 | self.camerabin.emit("start-capture") |
---|
68 | |
---|
69 | def __start_recording_cb(self, button): |
---|
70 | self.camerabin.set_property("mode", 2) # 1: Photo, 2: Video |
---|
71 | self.camerabin.set_property("location", "video.ogv") |
---|
72 | self.camerabin.emit("start-capture") |
---|
73 | |
---|
74 | def __stop_recording_cb(self, button): |
---|
75 | self.camerabin.emit("stop-capture") |
---|
76 | |
---|
77 | |
---|
78 | start=Main() |
---|
79 | Gtk.main() |
---|