From: Einar Jørgen Haraldseid Date: Thu, 19 Jun 2014 15:50:22 +0000 (+0200) Subject: Added an example for video in GUI X-Git-Url: https://git.slaskete.net/python-gstreamer-examples/commitdiff_plain/d2e7dae3443888c79e5dbb5d377f36f37b0b1f13?ds=inline Added an example for video in GUI --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..06661e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Ignore Glade's temp files +*~ diff --git a/example3.glade b/example3.glade new file mode 100644 index 0000000..8a73a3b --- /dev/null +++ b/example3.glade @@ -0,0 +1,117 @@ + + + + + + False + GStreamer video test + video-display + + + True + False + vertical + + + True + False + 8 + 8 + 8 + + + viewport + 320 + 240 + True + True + False + True + True + + + + + False + True + 0 + + + + + True + False + 8 + 8 + 8 + 8 + 1 + + + True + False + 8 + True + bottom + center + + + gtk-media-play + True + True + True + True + True + + + + False + True + 0 + + + + + gtk-media-stop + True + True + True + True + True + + + + False + True + 1 + + + + + gtk-quit + True + True + True + True + True + + + + False + True + 2 + + + + + + + False + True + 1 + + + + + + diff --git a/example3.py b/example3.py new file mode 100755 index 0000000..dd99993 --- /dev/null +++ b/example3.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python +# Let's see if we can get a viewport working, building on code from example2.py +# Some final info and updated code stolen from +# http://bazaar.launchpad.net/~jderose/+junk/gst-examples/view/head:/video-player-1.0 + +# GdkX11 to get access to xid, GstVideo to get access to set_window_handle +from gi.repository import Gtk, Gst, GdkX11, GstVideo +import signal + +class Main: + def __init__(self): + + # Create gui bits and bobs + + self.mainwindow = Gtk.Builder() + self.mainwindow.add_from_file("example3.glade") + + signals = { + "on_play_clicked" : self.OnPlay, + "on_stop_clicked" : self.OnStop, + "on_quit_clicked" : self.OnQuit, + } + + self.mainwindow.connect_signals(signals) + + # Create GStreamer bits and bobs + + # Initiate the pipeline + Gst.init(None) + self.pipeline = Gst.Pipeline("mypipeline") + + # Add a videotestsrc element to the pipeline, set it to pattern "snow." + self.videotestsrc = Gst.ElementFactory.make("videotestsrc", "videosource") + self.videotestsrc.set_property("pattern", "snow") + self.pipeline.add(self.videotestsrc) + + # Add a capsfilter that we want to apply to our videotestsrc + self.videotestcaps = Gst.ElementFactory.make("capsfilter", "videotestcaps") + self.videotestcaps.set_property("caps",Gst.Caps.from_string("video/x-raw,width=640,height=480")) + self.pipeline.add(self.videotestcaps) + + # Link the capsfilter to the videotestsrc + self.videotestsrc.link(self.videotestcaps) + + # Add a videosink element to the pipeline + self.videosink = Gst.ElementFactory.make("autovideosink", "videosink") + self.pipeline.add(self.videosink) + + # Link the already linked videotestcaps to the sink + self.videotestcaps.link(self.videosink) + + # Set up a bus to our pipeline to get notified when the video is ready + self.bus = self.pipeline.get_bus() + self.bus.enable_sync_message_emission() + self.bus.connect("sync-message::element", self.OnSyncElement) + + # Summon the window and connect the window's close button to quit + self.window = self.mainwindow.get_object("mainwindow") + self.window.connect("delete-event", Gtk.main_quit) + self.window.show_all() + + # Get window ID of the viewport widget from the GUI + self.win_id = self.mainwindow.get_object("viewport").get_window().get_xid() + + + # When we get a message that video is ready to display, set the + # correct window id to hook it to our viewport + def OnSyncElement(self, bus, message): + if message.get_structure().get_name() == "prepare-window-handle": + print "prepare-window-handle" + message.src.set_window_handle(self.win_id) + + def OnPlay(self, widget): + print "play" + self.pipeline.set_state(Gst.State.PLAYING) + + def OnStop(self, widget): + print "stop" + self.pipeline.set_state(Gst.State.READY) + + def OnQuit(self, widget): + print "quit" + Gtk.main_quit() + + # Workaround to get Ctrl+C to terminate from command line + # ref: https://bugzilla.gnome.org/show_bug.cgi?id=622084#c12 + signal.signal(signal.SIGINT, signal.SIG_DFL) + +start = Main() +Gtk.main()