Back to development
[platform/upstream/gstreamer.git] / subprojects / gst-python / old_examples / bps.py
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., 51 Franklin Street, Fifth Floor,
21 # Boston, MA 02110-1301, USA.
22
23 # Author: David I. Lehn <dlehn@users.sourceforge.net>
24 #
25
26 import pygtk
27 pygtk.require('2.0')
28
29 import sys
30 import time
31 import gobject
32 import gtk
33
34 import pygst
35 pygst.require('0.10')
36
37 import gst
38
39 class BPS(object):
40     def __init__(self):
41         self.buffers = 0
42         self.start = 0
43
44     def done(self):
45         end = time.time()
46         dt = end - self.start
47         bps = self.buffers/dt
48         spb = dt/self.buffers
49         print '\t%d buffers / %fs\t= %f bps\t= %f spb' % (self.buffers, dt, bps, spb)
50
51     def fakesrc(self, buffers):
52         src = gst.element_factory_make('fakesrc','src')
53         src.set_property('silent', 1)
54         src.set_property('num_buffers', buffers)
55         return src
56
57     def fakesink(self):
58         sink = gst.element_factory_make('fakesink','sink')
59         sink.set_property('silent', 1)
60         return sink
61
62     def build_pipeline(self, buffers):
63         pipeline = gst.Pipeline('pipeline')
64
65         src = self.fakesrc(buffers)
66         pipeline.add(src)
67         sink = self.fakesink()
68         pipeline.add(sink)
69         src.link(sink)
70
71         return pipeline
72
73     def idle(self, pipeline):
74         return pipeline.iterate()
75
76     def test(self):
77         self.bus = self.pipeline.get_bus()
78
79         self.start = time.time()
80         
81         self.pipeline.set_state(gst.STATE_PLAYING)
82
83         while 1:
84             msg = self.bus.poll(gst.MESSAGE_EOS | gst.MESSAGE_ERROR, gst.SECOND)
85             if msg:
86                 break
87
88         self.pipeline.set_state(gst.STATE_NULL)
89         self.done()
90
91     def run(self, buffers):
92         self.buffers = buffers
93         
94         print '# Testing buffer processing rate for "fakesrc ! fakesink"'
95         print '# bps = buffers per second'
96         print '# spb = seconds per buffer'
97         
98         self.pipeline = self.build_pipeline(buffers)
99         assert self.pipeline
100
101         self.test()
102     
103 def main(args):
104     "GStreamer Buffers-Per-Second tester"
105
106     if len(args) < 2:
107         print 'usage: %s buffers' % args[0]
108         return 1
109     
110     bps = BPS()
111     
112     buffers = int(args[1])
113     if buffers < 1:
114         print 'buffers must be higher than 0'
115         return
116
117     bps.run(buffers)
118
119 if __name__ == '__main__':
120     sys.exit(main(sys.argv))