Difference between revisions of "Development Team/Almanac/GStreamer"
< Development Team | Almanac
Jump to navigation
Jump to search
Line 7: | Line 7: | ||
=== How can I play back video files in my activity? === | === How can I play back video files in my activity? === | ||
Sugar, by default, can play back audio (encoded in Vorbis) and video (encoded in Theora) files in ogg containers. | Sugar, by default, can play back audio (encoded in Vorbis) and video (encoded in Theora) files in ogg containers. | ||
+ | |||
+ | The following file will be helpful for embedding video or audio files into your activity. After the code posting is a code snippet showing how to use this. | ||
+ | |||
+ | <pre> | ||
+ | import gtk | ||
+ | import pygtk | ||
+ | pygtk.require('2.0') | ||
+ | import sys | ||
+ | import pygst | ||
+ | pygst.require('0.10') | ||
+ | import gst | ||
+ | import gst.interfaces | ||
+ | import gobject | ||
+ | import time | ||
+ | gobject.threads_init() | ||
+ | |||
+ | |||
+ | class Gplay: | ||
+ | |||
+ | def __init__(self): | ||
+ | self.window = None | ||
+ | self.playing = False | ||
+ | |||
+ | self.player = gst.element_factory_make("playbin", "playbin") | ||
+ | xis = gst.element_factory_make("xvimagesink", "xvimagesink") | ||
+ | self.player.set_property("video-sink", xis) | ||
+ | bus = self.player.get_bus() | ||
+ | bus.enable_sync_message_emission() | ||
+ | bus.add_signal_watch() | ||
+ | self.SYNC_ID = bus.connect('sync-message::element', self._onSyncMessageCb) | ||
+ | |||
+ | |||
+ | def _onSyncMessageCb(self, bus, message): | ||
+ | if message.structure is None: | ||
+ | return True | ||
+ | if message.structure.get_name() == 'prepare-xwindow-id': | ||
+ | self.window.set_sink(message.src) | ||
+ | message.src.set_property('force-aspect-ratio', True) | ||
+ | return True | ||
+ | |||
+ | |||
+ | def setFile(self, path): | ||
+ | uri = "file://" + str( path ) | ||
+ | if (self.player.get_property('uri') == uri): | ||
+ | self.seek(gst.SECOND*0) | ||
+ | return | ||
+ | |||
+ | self.player.set_state(gst.STATE_READY) | ||
+ | self.player.set_property('uri', uri) | ||
+ | ext = uri[len(uri)-3:] | ||
+ | if (ext == "jpg"): | ||
+ | self.pause() | ||
+ | else: | ||
+ | self.play() | ||
+ | |||
+ | |||
+ | def queryPosition(self): | ||
+ | #"Returns a (position, duration) tuple" | ||
+ | try: | ||
+ | position, format = self.player.query_position(gst.FORMAT_TIME) | ||
+ | except: | ||
+ | position = gst.CLOCK_TIME_NONE | ||
+ | |||
+ | try: | ||
+ | duration, format = self.player.query_duration(gst.FORMAT_TIME) | ||
+ | except: | ||
+ | duration = gst.CLOCK_TIME_NONE | ||
+ | |||
+ | return (position, duration) | ||
+ | |||
+ | |||
+ | def seek(self, time): | ||
+ | event = gst.event_new_seek(1.0, gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE, gst.SEEK_TYPE_SET, time, gst.SEEK_TYPE_NONE, 0) | ||
+ | res = self.player.send_event(event) | ||
+ | if res: | ||
+ | self.player.set_new_stream_time(0L) | ||
+ | |||
+ | |||
+ | def pause(self): | ||
+ | self.playing = False | ||
+ | self.player.set_state(gst.STATE_PAUSED) | ||
+ | |||
+ | |||
+ | def play(self): | ||
+ | self.playing = True | ||
+ | self.player.set_state(gst.STATE_PLAYING) | ||
+ | |||
+ | |||
+ | def stop(self): | ||
+ | self.playing = False | ||
+ | self.player.set_state(gst.STATE_NULL) | ||
+ | self.nextMovie() | ||
+ | |||
+ | |||
+ | def get_state(self, timeout=1): | ||
+ | return self.player.get_state(timeout=timeout) | ||
+ | |||
+ | |||
+ | def is_playing(self): | ||
+ | return self.playing | ||
+ | |||
+ | |||
+ | class PlayVideoWindow(gtk.Window): | ||
+ | def __init__(self): | ||
+ | gtk.Window.__init__(self) | ||
+ | |||
+ | self.imagesink = None | ||
+ | self.unset_flags(gtk.DOUBLE_BUFFERED) | ||
+ | self.set_flags(gtk.APP_PAINTABLE) | ||
+ | |||
+ | |||
+ | 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) | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | And here is a code snippet showing how you would use this file to embed media playback in your activity: | ||
+ | |||
+ | <pre> | ||
+ | #this is our video playback window | ||
+ | self.gplay = Gplay( ) | ||
+ | self.gplayWin = PlayVideoWindow( ) | ||
+ | self.gplay.window = self.gplayWin | ||
+ | |||
+ | self.gplayWin.set_type_hint( gtk.gdk.WINDOW_TYPE_HINT_DIALOG ) | ||
+ | self.gplayWin.set_decorated( False ) | ||
+ | self.gplayWin.set_transient_for( self ) #activity subclass | ||
+ | self.gplayWin.move( 100, 100 ) | ||
+ | self.gplayWin.resize( 300, 400 ) | ||
+ | self.gplayWin.show_all( ) | ||
+ | </pre> |
Revision as of 17:10, 2 October 2008
For Developers: almanac · api · bugs · gitorious · cgit · download · people · OLPC: wiki · activities · trac · cgit · build index · repository · firmware · Fedora: packages
Where can I get additional resources and sample code on using the camera?
How can I play back video files in my activity?
Sugar, by default, can play back audio (encoded in Vorbis) and video (encoded in Theora) files in ogg containers.
The following file will be helpful for embedding video or audio files into your activity. After the code posting is a code snippet showing how to use this.
import gtk import pygtk pygtk.require('2.0') import sys import pygst pygst.require('0.10') import gst import gst.interfaces import gobject import time gobject.threads_init() class Gplay: def __init__(self): self.window = None self.playing = False self.player = gst.element_factory_make("playbin", "playbin") xis = gst.element_factory_make("xvimagesink", "xvimagesink") self.player.set_property("video-sink", xis) bus = self.player.get_bus() bus.enable_sync_message_emission() bus.add_signal_watch() self.SYNC_ID = bus.connect('sync-message::element', self._onSyncMessageCb) def _onSyncMessageCb(self, bus, message): if message.structure is None: return True if message.structure.get_name() == 'prepare-xwindow-id': self.window.set_sink(message.src) message.src.set_property('force-aspect-ratio', True) return True def setFile(self, path): uri = "file://" + str( path ) if (self.player.get_property('uri') == uri): self.seek(gst.SECOND*0) return self.player.set_state(gst.STATE_READY) self.player.set_property('uri', uri) ext = uri[len(uri)-3:] if (ext == "jpg"): self.pause() else: self.play() def queryPosition(self): #"Returns a (position, duration) tuple" try: position, format = self.player.query_position(gst.FORMAT_TIME) except: position = gst.CLOCK_TIME_NONE try: duration, format = self.player.query_duration(gst.FORMAT_TIME) except: duration = gst.CLOCK_TIME_NONE return (position, duration) def seek(self, time): event = gst.event_new_seek(1.0, gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE, gst.SEEK_TYPE_SET, time, gst.SEEK_TYPE_NONE, 0) res = self.player.send_event(event) if res: self.player.set_new_stream_time(0L) def pause(self): self.playing = False self.player.set_state(gst.STATE_PAUSED) def play(self): self.playing = True self.player.set_state(gst.STATE_PLAYING) def stop(self): self.playing = False self.player.set_state(gst.STATE_NULL) self.nextMovie() def get_state(self, timeout=1): return self.player.get_state(timeout=timeout) def is_playing(self): return self.playing class PlayVideoWindow(gtk.Window): def __init__(self): gtk.Window.__init__(self) self.imagesink = None self.unset_flags(gtk.DOUBLE_BUFFERED) self.set_flags(gtk.APP_PAINTABLE) 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)
And here is a code snippet showing how you would use this file to embed media playback in your activity:
#this is our video playback window self.gplay = Gplay( ) self.gplayWin = PlayVideoWindow( ) self.gplay.window = self.gplayWin self.gplayWin.set_type_hint( gtk.gdk.WINDOW_TYPE_HINT_DIALOG ) self.gplayWin.set_decorated( False ) self.gplayWin.set_transient_for( self ) #activity subclass self.gplayWin.move( 100, 100 ) self.gplayWin.resize( 300, 400 ) self.gplayWin.show_all( )