qtdemux: Don't emit GstSegment correcting start time when in MSE mode
[platform/upstream/gstreamer.git] / subprojects / gst-python / old_examples / filesrc.py
1 #!/usr/bin/env python
2 # -*- Mode: Python -*-
3 # vi:si:et:sw=4:sts=4:ts=4
4
5 # GStreamer python bindings
6 # Copyright (C) 2002 David I. Lehn <dlehn@users.sourceforge.net>
7 #               2004 Johan Dahlin  <johan@gnome.org>
8 #
9 # filesrc.py: implements a file source element completely in python
10 #
11 # This library is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU Library General Public
13 # License as published by the Free Software Foundation; either
14 # version 2 of the License, or (at your option) any later version.
15 #
16 # This library is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 # Library General Public License for more details.
20 #
21 # You should have received a copy of the GNU Library General Public
22 # License along with this library; if not, write to the
23 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 # Boston, MA 02110-1301, USA.
25
26 import sys
27 import gobject; gobject.threads_init()
28 import pygst
29 pygst.require('0.10')
30 import gst
31
32 class FileSource(gst.BaseSrc):
33     __gsttemplates__ = (
34         gst.PadTemplate("src",
35                         gst.PAD_SRC,
36                         gst.PAD_ALWAYS,
37                         gst.caps_new_any()),
38         )
39
40     blocksize = 4096
41     fd = None
42     
43     def __init__(self, name):
44         self.__gobject_init__()
45         self.curoffset = 0
46         self.set_name(name)
47             
48     def set_property(self, name, value):
49         if name == 'location':
50             self.fd = open(value, 'r')
51
52     def do_create(self, offset, size):
53         if offset != self.curoffset:
54             self.fd.seek(offset, 0)
55         data = self.fd.read(self.blocksize)
56         if data:
57             self.curoffset += len(data)
58             return gst.FLOW_OK, gst.Buffer(data)
59         else:
60             return gst.FLOW_UNEXPECTED, None
61 gobject.type_register(FileSource)
62
63 def main(args):
64     if len(args) != 3:
65         print 'Usage: %s input output' % (args[0])
66         return -1
67     
68     bin = gst.Pipeline('pipeline')
69
70     filesrc = FileSource('filesource')
71     assert filesrc
72     filesrc.set_property('location', args[1])
73    
74     filesink = gst.element_factory_make('filesink', 'sink')
75     filesink.set_property('location', args[2])
76
77     bin.add(filesrc, filesink)
78     gst.element_link_many(filesrc, filesink)
79     
80     bin.set_state(gst.STATE_PLAYING);
81     mainloop = gobject.MainLoop()
82
83     def bus_event(bus, message):
84         t = message.type
85         if t == gst.MESSAGE_EOS:
86             mainloop.quit()
87         elif t == gst.MESSAGE_ERROR:
88             err, debug = message.parse_error()
89             print "Error: %s" % err, debug
90             mainloop.quit()           
91         return True
92     bin.get_bus().add_watch(bus_event)
93
94     mainloop.run()
95     bin.set_state(gst.STATE_NULL)
96
97 if __name__ == '__main__':
98    sys.exit(main(sys.argv))
99