Merging gst-python
[platform/upstream/gstreamer.git] / old_examples / cutter.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) 2005 Thomas Vander Stichele
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 import gst
24 import time
25
26 import gobject
27 #gobject.threads_init() # so we can safely receive signals from threads
28
29 count = 0
30
31 def on_message_application(cutter, message, loop):
32     global count
33     s = message.structure
34     which = 'below'
35     if s['above']: which = 'above'
36     print "%s: %s threshold" % (gst.TIME_ARGS(s['timestamp']), which)
37     if s['above']: count += 1
38     if count > 2: loop.quit()
39
40 def main():
41     type = 'async'
42     loop = gobject.MainLoop()
43
44     pipeline = gst.Pipeline("cutter")
45     src = gst.element_factory_make("sinesrc", "src")
46     cutter = gst.element_factory_make("cutter")
47     cutter.set_property('threshold', 0.5)
48     sink = gst.element_factory_make("fakesink", "sink")
49     pipeline.add(src, cutter, sink)
50     src.link(cutter)
51     cutter.link(sink)
52
53     control = gst.Controller(src, "volume")
54     control.set_interpolation_mode("volume", gst.INTERPOLATE_LINEAR)
55
56     control.set("volume", 0, 0.0)
57     control.set("volume", 2 * gst.SECOND, 1.0)
58     control.set("volume", 4 * gst.SECOND, 0.0)
59     control.set("volume", 6 * gst.SECOND, 1.0)
60     control.set("volume", 8 * gst.SECOND, 0.0)
61     control.set("volume", 10 * gst.SECOND, 1.0)
62
63     bus = pipeline.get_bus()
64
65     if type == 'async':
66         bus.add_signal_watch()
67         bus.connect('message::element', on_message_application, loop)
68     else:
69         # FIXME: needs wrapping in gst-python
70         bus.set_sync_handler(bus.sync_signal_handler)
71         bus.connect('sync-message::element', on_message_application, loop)
72
73     pipeline.set_state(gst.STATE_PLAYING)
74
75     loop.run()
76
77     pipeline.set_state(gst.STATE_NULL)
78
79 if __name__ == "__main__":
80     main()