~ubuntu-branches/ubuntu/saucy/freemix/saucy

« back to all changes in this revision

Viewing changes to src/engine.py

  • Committer: Bazaar Package Importer
  • Author(s): Luis de Bethencourt
  • Date: 2009-02-14 17:04:04 UTC
  • Revision ID: james.westby@ubuntu.com-20090214170404-9dmt4t7qljg2qn23
Tags: upstream-0.2
ImportĀ upstreamĀ versionĀ 0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# Copyright (C) 2008 Luis de Bethencourt
 
4
# <luisbg@ubuntu.com>
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2, or (at your option)
 
9
# any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 
19
# USA
 
20
 
 
21
"""freemix engine class"""
 
22
 
 
23
import gobject
 
24
gobject.threads_init() 
 
25
import gst
 
26
import time
 
27
 
 
28
class Engine:
 
29
    '''freemix engine class. Encapsulates all the core gstreamer work
 
30
       in nice function per feature for the frontend.''' 
 
31
 
 
32
    def __init__(self):
 
33
        self.running = False
 
34
        # print "init engine"
 
35
        self.speed = 1.0
 
36
 
 
37
    def start(self, filesrc):
 
38
        self.running = True 
 
39
 
 
40
        def bus_handler(unused_bus, message):
 
41
            # print message.type
 
42
            if message.type == gst.MESSAGE_SEGMENT_DONE:
 
43
                self.SeekToLocation(0)
 
44
            if message.type == gst.MESSAGE_EOS:
 
45
                self.AsyncDone()
 
46
            if message.type == gst.MESSAGE_TAG:
 
47
                self.SeekToLocation(0)
 
48
            if message.type == gst.MESSAGE_ERROR:
 
49
                print "ERROR"
 
50
            return gst.BUS_PASS
 
51
 
 
52
        # create our pipeline
 
53
        self.pipeline = gst.Pipeline()
 
54
        self.bus = self.pipeline.get_bus()
 
55
        self.bus.add_signal_watch()
 
56
        self.bus.connect ('message', bus_handler)
 
57
 
 
58
        self.bin1 = self.VideoBin(filesrc)
 
59
 
 
60
        self.imagesink = gst.element_factory_make("xvimagesink", "imagesink")
 
61
        self.imagesink.set_property("force-aspect-ratio", True)
 
62
        self.imagesink.set_property("handle-expose", True)
 
63
        self.pipeline.add(self.bin1, self.imagesink)
 
64
 
 
65
        self.bin1.link(self.imagesink)
 
66
 
 
67
        self.first = True
 
68
        self.pipeline.set_state(gst.STATE_PLAYING)
 
69
 
 
70
    def VideoBin(self, filesrc):
 
71
        self.bin = gst.Bin()
 
72
        self.src = gst.element_factory_make("filesrc", "src")
 
73
        self.bin.add(self.src)
 
74
        self.src.set_property("location", filesrc)
 
75
        
 
76
        self.decodebin = gst.element_factory_make("decodebin", "decodebin")
 
77
        self.decodebin.connect("new-decoded-pad", self.OnDynamicPad)
 
78
        self.bin.add(self.decodebin)
 
79
        
 
80
        self.src.link(self.decodebin)
 
81
 
 
82
        self.colorspace = gst.element_factory_make("ffmpegcolorspace", "colorspace")
 
83
 
 
84
        self.vqueue = gst.element_factory_make("queue", "vqueue")
 
85
        self.vqueue.set_property ("max-size-buffers", 3)
 
86
        
 
87
        self.videoscale = gst.element_factory_make("videoscale", "videoscale")
 
88
 
 
89
        self.bin.add(self.colorspace, self.vqueue, self.videoscale)
 
90
 
 
91
        self.colorspace.link(self.vqueue)
 
92
        self.vqueue.link(self.videoscale)
 
93
 
 
94
        target = self.videoscale.get_pad("src")
 
95
        self.sinkpad = gst.GhostPad("sink", target)
 
96
        self.sinkpad.set_active(True)
 
97
        self.bin.add_pad(self.sinkpad)
 
98
 
 
99
        return self.bin
 
100
 
 
101
    def OnDynamicPad(self, dbin, pad, islast):
 
102
        pad.link(self.colorspace.get_pad("sink"))
 
103
        # print "OnDynamicPad called"
 
104
 
 
105
    def AsyncDone(self):
 
106
        seek = self.pipeline.seek (self.speed, gst.FORMAT_TIME, \
 
107
            gst.SEEK_FLAG_SEGMENT | gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_ACCURATE, \
 
108
            gst.SEEK_TYPE_SET, 0, gst.SEEK_TYPE_NONE, 0)
 
109
        # print "Async " + str(self.speed) 
 
110
 
 
111
    def SeekToLocation(self, location):
 
112
        self.pipeline.seek(self.speed, gst.FORMAT_TIME, \
 
113
            gst.SEEK_FLAG_SEGMENT, gst.SEEK_TYPE_SET, 0, gst.SEEK_TYPE_NONE, \
 
114
            location)
 
115
        # print "seek to %r" % location
 
116
 
 
117
    def switchVideo(self, filesrc):
 
118
        self.pipeline.set_state(gst.STATE_READY)
 
119
        self.src.set_property("location", filesrc)
 
120
        self.pipeline.set_state(gst.STATE_PLAYING)
 
121
        self.SeekToLocation(0)
 
122
 
 
123
    def play(self, filesrc, speed):
 
124
        # print "engine got call to play: " + filesrc
 
125
        self.speed = speed
 
126
        if self.running == False:
 
127
            self.start(filesrc)
 
128
        else:
 
129
            self.switchVideo(filesrc) 
 
130
 
 
131
 
 
132
if __name__ == "__main__":
 
133
    import os, optparse
 
134
 
 
135
    usage = """ engine.py -i [file]"""
 
136
 
 
137
    parser = optparse.OptionParser(usage=usage)
 
138
    parser.add_option("-i", "--input", action="store", type="string", dest="input", help="Input video file", default="")
 
139
    (options, args) = parser.parse_args()
 
140
 
 
141
    print "Playing: %r" % options.input
 
142
 
 
143
    engine = Engine(options.input)
 
144
    gobject.MainLoop().run()