~ubuntu-branches/ubuntu/quantal/gst0.10-python/quantal

« back to all changes in this revision

Viewing changes to examples/maemogst.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2010-01-27 08:00:50 UTC
  • mfrom: (1.1.23 upstream) (7.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100127080050-2iu7igaxot48lrhj
* New upstream pre-release:
  + debian/control:
    - Update dependencies.
  + Fixes importing on kFreeBSD (Closes: #563535).
  + Fixes "'module' object has no attribute 'Element'" import
    error (Closes: #531347).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import gobject
 
2
gobject.threads_init()
 
3
import gtk
 
4
gtk.gdk.threads_init()
 
5
import hildon
 
6
import gst
 
7
import sys
 
8
 
 
9
# VideoWidget taken from play.py in gst-python examples
 
10
class VideoWidget(gtk.DrawingArea):
 
11
    def __init__(self):
 
12
        gtk.DrawingArea.__init__(self)
 
13
        self.imagesink = None
 
14
        self.unset_flags(gtk.DOUBLE_BUFFERED)
 
15
 
 
16
    def do_expose_event(self, event):
 
17
        if self.imagesink:
 
18
            self.imagesink.expose()
 
19
            return False
 
20
        else:
 
21
            return True
 
22
 
 
23
    def set_sink(self, sink):
 
24
        assert self.window.xid
 
25
        self.imagesink = sink
 
26
        self.imagesink.set_xwindow_id(self.window.xid)
 
27
 
 
28
class MaemoGstView:
 
29
 
 
30
    def __init__(self):
 
31
        # hildon has one program instance per app, so get instance
 
32
        self.p = hildon.Program.get_instance()
 
33
        # set name of application: this shows in titlebar
 
34
        gtk.set_application_name("Maemo GStreamer VideoTest")
 
35
        # stackable window in case we want more windows in future in app
 
36
        self.w = hildon.StackableWindow()
 
37
        box = gtk.VBox()
 
38
        self.video_widget = VideoWidget()
 
39
        # video widget we want to expand to size
 
40
        box.pack_start(self.video_widget, True, True, 0)
 
41
        # a button finger height to play/pause 
 
42
        self.button = hildon.Button(gtk.HILDON_SIZE_FINGER_HEIGHT,
 
43
            hildon.BUTTON_ARRANGEMENT_VERTICAL, title="Pause")
 
44
        self.button.connect_after("clicked", self.on_button_clicked)
 
45
        # don't want button to expand or fill, just stay finger height
 
46
        box.pack_start(self.button, False, False, 0)
 
47
        self.w.add(box)
 
48
        self.w.connect("delete-event", gtk.main_quit)
 
49
        self.p.add_window(self.w)
 
50
        self.w.show_all()
 
51
        self.start_streaming()
 
52
 
 
53
    def start_streaming(self):
 
54
        # we use ximagesink solely for screenshotting ability
 
55
        # less cpu usage would happen with videotestsrc ! xvimagesink
 
56
        self.pipeline = \
 
57
            gst.parse_launch("videotestsrc ! videoscale ! ximagesink")
 
58
        bus = self.pipeline.get_bus()
 
59
        # need to connect to sync message handler so we get the sink to be
 
60
        # embedded at the right time and not have a temporary new window
 
61
        bus.enable_sync_message_emission()
 
62
        bus.add_signal_watch()
 
63
        bus.connect("sync-message::element", self.on_sync_message)
 
64
        bus.connect("message", self.on_message)
 
65
        self.pipeline.set_state(gst.STATE_PLAYING)
 
66
 
 
67
    def on_sync_message(self, bus, message):
 
68
        if message.structure is None:
 
69
            return
 
70
        if message.structure.get_name() == 'prepare-xwindow-id':
 
71
            # all this is needed to sync with the X server before giving the
 
72
            # x id to the sink
 
73
            gtk.gdk.threads_enter()
 
74
            gtk.gdk.display_get_default().sync()
 
75
            self.video_widget.set_sink(message.src)
 
76
            message.src.set_property("force-aspect-ratio", True)
 
77
            gtk.gdk.threads_leave()
 
78
 
 
79
    def on_message(self, bus, message):
 
80
        if message.type == gst.MESSAGE_ERROR:
 
81
            err, debug = message.parse_error()
 
82
            hildon.hildon_banner_show_information(self.w, '', 
 
83
                "Error: %s" % err)
 
84
 
 
85
    def on_button_clicked(self, widget):
 
86
        success, state, pending = self.pipeline.get_state(1)
 
87
        # do not listen if in middle of state change
 
88
        if not pending:
 
89
            if state == gst.STATE_PLAYING:
 
90
                self.pipeline.set_state(gst.STATE_PAUSED)
 
91
                self.button.set_label("Play")
 
92
            else:
 
93
                self.pipeline.set_state(gst.STATE_PLAYING)
 
94
                self.button.set_label("Pause")
 
95
 
 
96
def main():
 
97
    view = MaemoGstView()
 
98
    gtk.main()
 
99
 
 
100
if __name__ == '__main__':
 
101
    sys.exit(main())