home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / python-gst0.10 / examples / bps.py < prev    next >
Encoding:
Python Source  |  2006-07-31  |  3.0 KB  |  121 lines

  1. #!/usr/bin/env python
  2. # -*- Mode: Python -*-
  3. # vi:si:et:sw=4:sts=4:ts=4
  4.  
  5. # gst-python
  6. # Copyright (C) 2003 David I. Lehn
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Library General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. # Library General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Library General Public
  19. # License along with this library; if not, write to the
  20. # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. # Boston, MA 02111-1307, USA.
  22. # Author: David I. Lehn <dlehn@users.sourceforge.net>
  23. #
  24.  
  25. import pygtk
  26. pygtk.require('2.0')
  27.  
  28. import sys
  29. import time
  30. import gobject
  31. import gtk
  32.  
  33. import pygst
  34. pygst.require('0.10')
  35.  
  36. import gst
  37.  
  38. class BPS(object):
  39.     def __init__(self):
  40.         self.buffers = 0
  41.         self.start = 0
  42.  
  43.     def done(self):
  44.         end = time.time()
  45.         dt = end - self.start
  46.         bps = self.buffers/dt
  47.         spb = dt/self.buffers
  48.         print '\t%d buffers / %fs\t= %f bps\t= %f spb' % (self.buffers, dt, bps, spb)
  49.  
  50.     def fakesrc(self, buffers):
  51.         src = gst.element_factory_make('fakesrc','src')
  52.         src.set_property('silent', 1)
  53.         src.set_property('num_buffers', buffers)
  54.         return src
  55.  
  56.     def fakesink(self):
  57.         sink = gst.element_factory_make('fakesink','sink')
  58.         sink.set_property('silent', 1)
  59.         return sink
  60.  
  61.     def build_pipeline(self, buffers):
  62.         pipeline = gst.Pipeline('pipeline')
  63.  
  64.         src = self.fakesrc(buffers)
  65.         pipeline.add(src)
  66.         sink = self.fakesink()
  67.         pipeline.add(sink)
  68.         src.link(sink)
  69.  
  70.         return pipeline
  71.  
  72.     def idle(self, pipeline):
  73.         return pipeline.iterate()
  74.  
  75.     def test(self):
  76.         self.bus = self.pipeline.get_bus()
  77.  
  78.         self.start = time.time()
  79.         
  80.         self.pipeline.set_state(gst.STATE_PLAYING)
  81.  
  82.         while 1:
  83.             msg = self.bus.poll(gst.MESSAGE_EOS | gst.MESSAGE_ERROR, gst.SECOND)
  84.             if msg:
  85.                 break
  86.  
  87.         self.pipeline.set_state(gst.STATE_NULL)
  88.         self.done()
  89.  
  90.     def run(self, buffers):
  91.         self.buffers = buffers
  92.         
  93.         print '# Testing buffer processing rate for "fakesrc ! fakesink"'
  94.         print '# bps = buffers per second'
  95.         print '# spb = seconds per buffer'
  96.         
  97.         self.pipeline = self.build_pipeline(buffers)
  98.         assert self.pipeline
  99.  
  100.         self.test()
  101.     
  102. def main(args):
  103.     "GStreamer Buffers-Per-Second tester"
  104.  
  105.     if len(args) < 2:
  106.         print 'usage: %s buffers' % args[0]
  107.         return 1
  108.     
  109.     bps = BPS()
  110.     
  111.     buffers = int(args[1])
  112.     if buffers < 1:
  113.     print 'buffers must be higher than 0'
  114.     return
  115.  
  116.     bps.run(buffers)
  117.  
  118. if __name__ == '__main__':
  119.     sys.exit(main(sys.argv))
  120.