From 2488b32b0767ae6f00d2f48b9b229aa135d90a18 Mon Sep 17 00:00:00 2001 From: Edward Hervey Date: Wed, 13 Jul 2005 12:31:47 +0000 Subject: [PATCH] examples/: Updated some examples to 0.9 Original commit message from CVS: * examples/bps.py: * examples/f2f.py: * examples/gstfile.py: Updated some examples to 0.9 --- ChangeLog | 7 ++++ examples/bps.py | 66 +++++++++++---------------------- examples/f2f.py | 21 ++++++++--- examples/gstfile.py | 90 ++++++++++++++++++++++++++++++++++++--------- 4 files changed, 117 insertions(+), 67 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8836210f25..1de66f456c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2005-07-13 Edward Hervey + + * examples/bps.py: + * examples/f2f.py: + * examples/gstfile.py: + Updated some examples to 0.9 + 2005-07-13 Andy Wingo * examples/vumeter.py: New file, a VU meter application that reads diff --git a/examples/bps.py b/examples/bps.py index 82976f3714..1e582bbc7b 100755 --- a/examples/bps.py +++ b/examples/bps.py @@ -28,6 +28,10 @@ import sys import time import gobject import gtk + +import pygst +pygst.require('0.9') + import gst class BPS(object): @@ -42,11 +46,6 @@ class BPS(object): spb = dt/self.buffers print '\t%d buffers / %fs\t= %f bps\t= %f spb' % (self.buffers, dt, bps, spb) - def eos(self, sink): - self.done() - if self.method in ('gtk', 'c'): - gst.main_quit() - def fakesrc(self, buffers): src = gst.element_factory_make('fakesrc','src') src.set_property('silent', 1) @@ -65,78 +64,55 @@ class BPS(object): pipeline.add(src) sink = self.fakesink() pipeline.add(sink) - sink.connect('eos', self.eos) src.link(sink) return pipeline - def notify(self, sender, obj, arg): - prop = obj.get_property(arg.name) - print 'notify', sender, arg.name, prop - print prop - def idle(self, pipeline): return pipeline.iterate() - def test(self, method): - print '%s:' % (method,), - self.method = method - - print self.pipeline.get_state() - self.pipeline.set_state(gst.STATE_PLAYING) - print self.pipeline.get_state() + def test(self): + self.bus = self.pipeline.get_bus() - if method == 'py': - self.start = time.time() - while self.pipeline.iterate(): - pass - elif method == 'c': - self.start = time.time() - gobject.idle_add(self.pipeline.iterate) - gst.main() - #elif method == 'gst': - # self.start = time.time() - # gtk.idle_add(self.idle, self.pipeline) - # gtk.main() + self.start = time.time() + + self.pipeline.set_state(gst.STATE_PLAYING) + + while 1: + msg = self.bus.poll(gst.MESSAGE_EOS | gst.MESSAGE_ERROR, gst.SECOND) + if msg: + break self.pipeline.set_state(gst.STATE_NULL) + self.done() - def run(self, buffers, methods): + def run(self, buffers): self.buffers = buffers print '# Testing buffer processing rate for "fakesrc ! fakesink"' - #print '# gst = gtk idle loop function in python' - print '# c = gtk idle loop function in C' - print '# py = full iterate loop in python' - print '# all = full iterate loop in C' print '# bps = buffers per second' print '# spb = seconds per buffer' self.pipeline = self.build_pipeline(buffers) assert self.pipeline - #self.pipeline.connect('deep-notify', self.notify) - - map(self.test, methods) + + self.test() def main(args): "GStreamer Buffers-Per-Second tester" if len(args) < 2: - print 'usage: %s buffers [method method ...]' % args[0] + print 'usage: %s buffers' % args[0] return 1 bps = BPS() buffers = int(args[1]) - if buffers < 0: + if buffers < 1: print 'buffers must be higher than 0' return - methods = args[2:] - if not methods: - methods = ('gtk', 'c', 'py', 'all') - - bps.run(buffers, methods) + bps.run(buffers) if __name__ == '__main__': sys.exit(main(sys.argv)) diff --git a/examples/f2f.py b/examples/f2f.py index 56529abdea..fd94c00e5a 100755 --- a/examples/f2f.py +++ b/examples/f2f.py @@ -23,6 +23,9 @@ import sys +import pygst +pygst.require('0.9') + import gst def handoff_cb(sender, *args): @@ -31,18 +34,26 @@ def handoff_cb(sender, *args): def main(args): # create a new bin to hold the elements #gst_debug_set_categories(-1) - bin = gst.parse_launch('fakesrc name=source silent=1 num-buffers=10 ! ' + - 'fakesink name=sink silent=1') + bin = gst.parse_launch('fakesrc name=source silent=1 num-buffers=10 signal-handoffs=true ! ' + + 'fakesink name=sink silent=1 signal-handoffs=true') source = bin.get_by_name('source') source.connect('handoff', handoff_cb) - sink = bin.get_by_name('source') + source.get_pad("src").connect("have-data", handoff_cb) + sink = bin.get_by_name('sink') sink.connect('handoff', handoff_cb) + sink.get_pad("sink").connect('have-data', handoff_cb) + + print source, sink + + bus = bin.get_bus() res = bin.set_state(gst.STATE_PLAYING); assert res - while bin.iterate(): - pass + while 1: + msg = bus.poll(gst.MESSAGE_EOS | gst.MESSAGE_ERROR, gst.SECOND) + if msg: + break res = bin.set_state(gst.STATE_NULL) assert res diff --git a/examples/gstfile.py b/examples/gstfile.py index 49c0c59393..b97c397827 100644 --- a/examples/gstfile.py +++ b/examples/gstfile.py @@ -13,6 +13,10 @@ import os import sys import gobject + +import pygst +pygst.require('0.9') + import gst def time_to_string(value): @@ -104,7 +108,7 @@ class Discoverer(gst.Pipeline): self.typefind.connect("have-type", self._have_type_cb) self.dbin.connect("new-decoded-pad", self._new_decoded_pad_cb) self.dbin.connect("unknown-type", self._unknown_type_cb) - self.dbin.connect("found-tag", self._found_tag_cb) + #self.dbin.connect("found-tag", self._found_tag_cb) self.discover() @@ -112,11 +116,41 @@ class Discoverer(gst.Pipeline): """iterate on ourself to find the information on the given file""" if self.finished: return - self.set_state(gst.STATE_PLAYING) + if not self.set_state(gst.STATE_PLAYING): + # the pipeline wasn't able to be set to playing + self.finished = True + return + bus = self.get_bus() while 1: - if not self.iterate(): + if self.finished: + #print "self.finished, stopping" break - self.set_state(gst.STATE_NULL) + msg = bus.poll(gst.MESSAGE_ANY, gst.SECOND) + if msg: + msg = bus.pop() + else: + continue + if msg.type & gst.MESSAGE_STATE_CHANGED: + #print "state changed", msg.src.get_name() + #print msg.parse_state_changed() + pass + elif msg.type & gst.MESSAGE_EOS: + break + elif msg.type & gst.MESSAGE_TAG: + for key in msg.parse_tag().keys(): + self.tags[key] = msg.structure[key] + print msg.structure.to_string() + elif msg.type & gst.MESSAGE_ERROR: + print "whooops, error" + break + else: + print "unknown message type" + + print "going to PAUSED" + #self.set_state(gst.STATE_PAUSED) + print "going to ready" + #self.set_state(gst.STATE_READY) + print "now in ready" self.finished = True def print_info(self): @@ -159,18 +193,34 @@ class Discoverer(gst.Pipeline): print "%20s :\t" % tag, self.tags[tag] def _unknown_type_cb(self, dbin, pad, caps): + print "unknown type", caps.to_string() + # if we get an unknown type and we don't already have an + # audio or video pad, we are finished ! self.otherstreams.append(caps.to_string()) + if not self.is_video and not self.is_audio: + self.finished = True def _have_type_cb(self, typefind, prob, caps): self.mimetype = caps.to_string() def _notify_caps_cb(self, pad, args): caps = pad.get_negotiated_caps() + print "caps notify on", pad, ":", caps if not caps: return # the caps are fixed # We now get the total length of that stream - length = pad.get_peer().query(gst.QUERY_TOTAL, gst.FORMAT_TIME) + q = gst.query_new_position(gst.FORMAT_TIME) + #print "query refcount", q.__grefcount__ + if pad.get_peer().query(q): + #print "query refcount", q.__grefcount__ + length = q.structure["end"] + pos = q.structure["cur"] + format = q.structure["format"] + #print "got length", time_to_string(pos), time_to_string(length), format + else: + print "query didn't work" + #length = pad.get_peer().query(gst.QUERY_TOTAL, gst.FORMAT_TIME) # We store the caps and length in the proper location if "audio" in caps.to_string(): self.audiocaps = caps @@ -195,10 +245,23 @@ class Discoverer(gst.Pipeline): def _new_decoded_pad_cb(self, dbin, pad, is_last): # Does the file contain got audio or video ? - if "audio" in pad.get_caps().to_string(): + caps = pad.get_caps() + print "new decoded pad", caps.to_string() + if "audio" in caps.to_string(): self.is_audio = True - elif "video" in pad.get_caps().to_string(): + if caps.is_fixed(): + print "have negotiated caps", caps + self.audiocaps = caps + return + elif "video" in caps.to_string(): self.is_video = True + if caps.is_fixed(): + print "have negotiated caps", caps + self.videocaps = caps + return + else: + print "got a different caps..", caps + return if is_last and not self.is_video and not self.is_audio: self.finished = True return @@ -208,20 +271,13 @@ class Discoverer(gst.Pipeline): sinkpad = fakesink.get_pad("sink") # ... and connect a callback for when the caps are fixed sinkpad.connect("notify::caps", self._notify_caps_cb) - pad.link(sinkpad) - fakesink.set_state(gst.STATE_PLAYING) + if pad.link(sinkpad): + print "##### Couldn't link pad to fakesink" + #fakesink.set_state(gst.STATE_PLAYING) def _found_tag_cb(self, dbin, source, tags): self.tags.update(tags) - def do_iterate(self): - # this overrides the GstBin 'iterate' method - # if we have finished discovering we stop the iteration - if self.finished: - return False - # else we call the parent class method - return gst.Pipeline.do_iterate(self) - gobject.type_register(Discoverer) def main(args):