~osomon/moovida/pipestuff-youtube

« back to all changes in this revision

Viewing changes to pipes.py

  • Committer: Ugo Riboni
  • Date: 2009-12-11 18:13:34 UTC
  • Revision ID: uriboni@fluendo.com-20091211181334-rk4zzdlnbrt53zqs
Initial commit of prototype

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import gobject
 
2
 
 
3
DIRECTION_IN = 'in'
 
4
DIRECTION_OUT = 'out'
 
5
 
 
6
# TODO: use gobject type system
 
7
TYPE_ANY = 'any'
 
8
TYPE_VALUE = 'val'
 
9
TYPE_LIST = 'lst'
 
10
TYPE_OBJECT = 'obj'
 
11
TYPE_INTERNAL = 'int'
 
12
TYPE_EXTERN = 'ext'
 
13
 
 
14
class Pad(gobject.GObject):
 
15
    __gsignals__ = {
 
16
        'data-ready': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
 
17
    }
 
18
 
 
19
    def __init__(self, direction, type = TYPE_ANY, subtype = None):
 
20
        super(Pad, self).__init__()
 
21
        self.direction = direction
 
22
        self.type = type
 
23
        self.subtype = subtype
 
24
        self.owner = None
 
25
        self.linked = None
 
26
        
 
27
    def link(self, pad):
 
28
        if pad.direction == self.direction:
 
29
            print "pads with same direction (%s) can't be linked" % pad.direction
 
30
            return False
 
31
        
 
32
        if pad.owner is None or self.owner is None:
 
33
            print "both pads need to have an owner to be able to link them"
 
34
            return False
 
35
 
 
36
        #TODO: add checks for types
 
37
        #TODO: add checks for loops
 
38
        
 
39
        self.linked = pad
 
40
        pad.linked = self
 
41
        
 
42
        if pad.direction == DIRECTION_OUT:
 
43
            self.linked.connect('data-ready', self.owner.on_data_ready)
 
44
 
 
45
        return True
 
46
    
 
47
    def unlink(self):
 
48
        if self.linked is None:
 
49
            return True
 
50
        self.linked.linked = None
 
51
        self.linked = None
 
52
 
 
53
    def run(self):
 
54
        if not self.linked is None:
 
55
            self.linked.owner.run(self.linked)
 
56
 
 
57
    def data_ready(self):
 
58
        self.emit('data-ready')
 
59
        
 
60
    def data(self):
 
61
        return self.owner.data(self)
 
62
    
 
63
class Element(object):
 
64
    def __init__(self, name):
 
65
        self.name = name
 
66
        self._pads = {}
 
67
        
 
68
    def pad(self, name):
 
69
        return self._pads.get(name)
 
70
        
 
71
    def add_pad(self, name, pad):
 
72
        if self._pads.get(name) is None:
 
73
            self._pads[name] = pad
 
74
            pad.owner = self
 
75
            return True
 
76
        else:
 
77
            print "a pad with the name %s already exist. can't add another" % name
 
78
            return False
 
79
 
 
80
    def run(self, pad):
 
81
        print "default run() does nothing. re-implement in your element class"
 
82
        return True
 
83
 
 
84
    def data(self, pad=None):
 
85
        print "default data() returns None. re-implement in your element class"
 
86
        return None
 
87
    
 
88
    def on_data_ready(self, sender):
 
89
        print "default data_ready() does nothing. re-implement in your element class"
 
90
        return None
 
91
        
 
92
 
 
93
  
 
94
class Source(Element):
 
95
    def __init__(self, name, type, subtype = None):
 
96
        super(Source, self).__init__(name)
 
97
        self.add_pad('out', Pad(DIRECTION_OUT, type, subtype))
 
98
 
 
99
 
 
100
class Filter(Element):
 
101
    def __init__(self, name, in_type, out_type, in_subtype = None, out_subtype = None):
 
102
        super(Filter, self).__init__(name)
 
103
        self.add_pad('in', Pad(DIRECTION_IN, in_type, in_subtype))
 
104
        self.add_pad('out', Pad(DIRECTION_OUT, out_type, out_subtype))
 
105
        
 
106
class Output(Element):
 
107
    def __init__(self):
 
108
        super(Output, self).__init__("output")
 
109
        self.add_pad('in', Pad(DIRECTION_IN, TYPE_INTERNAL, None))
 
110
 
 
111
    def on_data_ready(self, sender):
 
112
        print "FINISHED !!!!"
 
113
        print sender.data()
 
114
 
 
115
    def run(self):
 
116
        self.pad("in").run()
 
117
        
 
118