summaryrefslogtreecommitdiff
path: root/testsuite/test_ghostpad.py
blob: 75a28cc443239c4fcfa1f1d91b485e6e56795adb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# gst-python - Python bindings for GStreamer
# Copyright (C) 2002 David I. Lehn
# Copyright (C) 2004 Johan Dahlin
# Copyright (C) 2005 Edward Hervey
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307  USA

from common import gst, unittest, TestCase, pygobject_2_13

import sys
import gc
import gobject

class SrcBin(gst.Bin):
    def prepare(self):
        src = gst.element_factory_make('fakesrc')
        self.add(src)
        pad = src.get_pad("src")
        ghostpad = gst.GhostPad("src", pad)
        self.add_pad(ghostpad)
gobject.type_register(SrcBin)

class SinkBin(gst.Bin):
    def prepare(self):
        sink = gst.element_factory_make('fakesink')
        self.add(sink)
        pad = sink.get_pad("sink")
        ghostpad = gst.GhostPad("sink", pad)
        self.add_pad(ghostpad)
        self.sink = sink

    def connect_handoff(self, cb, *args, **kwargs):
        self.sink.set_property('signal-handoffs', True)
        self.sink.connect('handoff', cb, *args, **kwargs)
        
gobject.type_register(SinkBin)

        
class PipeTest(TestCase):
    def setUp(self):
        gst.info("setUp")
        TestCase.setUp(self)
        self.pipeline = gst.Pipeline()
        self.assertEquals(self.pipeline.__gstrefcount__, 1)
        self.assertEquals(sys.getrefcount(self.pipeline), pygobject_2_13 and 2 or 3)

        self.src = SrcBin()
        self.src.prepare()
        self.sink = SinkBin()
        self.sink.prepare()
        self.assertEquals(self.src.__gstrefcount__, 1)
        self.assertEquals(sys.getrefcount(self.src), pygobject_2_13 and 2 or 3)
        self.assertEquals(self.sink.__gstrefcount__, 1)
        self.assertEquals(sys.getrefcount(self.sink), pygobject_2_13 and 2 or 3)
        gst.info("end of SetUp")

    def tearDown(self):
        gst.info("tearDown")
        self.assertTrue (self.pipeline.__gstrefcount__ >= 1 and self.pipeline.__gstrefcount__ <= 2)
        self.assertEquals(sys.getrefcount(self.pipeline), pygobject_2_13 and 2 or 3)
        self.assertEquals(self.src.__gstrefcount__, 2)
        self.assertEquals(sys.getrefcount(self.src), pygobject_2_13 and 2 or 3)
        self.assertEquals(self.sink.__gstrefcount__, 2)
        self.assertEquals(sys.getrefcount(self.sink), 3)
        gst.debug('deleting pipeline')
        del self.pipeline
        self.gccollect()

        self.assertEquals(self.src.__gstrefcount__, 1) # parent gone
        self.assertEquals(self.sink.__gstrefcount__, 1) # parent gone
        self.assertEquals(sys.getrefcount(self.src), pygobject_2_13 and 2 or 3)
        self.assertEquals(sys.getrefcount(self.sink), pygobject_2_13 and 2 or 3)
        gst.debug('deleting src')
        del self.src
        self.gccollect()
        gst.debug('deleting sink')
        del self.sink
        self.gccollect()

        TestCase.tearDown(self)
        
    def testBinState(self):
        self.pipeline.add(self.src, self.sink)
        self.src.link(self.sink)
        self.sink.connect_handoff(self._sink_handoff_cb)
        self._handoffs = 0

        self.assertTrue(self.pipeline.set_state(gst.STATE_PLAYING) != gst.STATE_CHANGE_FAILURE)
        while True:
            (ret, cur, pen) = self.pipeline.get_state()
            if ret == gst.STATE_CHANGE_SUCCESS and cur == gst.STATE_PLAYING:
                break

        while self._handoffs < 10:
                pass

        self.assertEquals(self.pipeline.set_state(gst.STATE_NULL), gst.STATE_CHANGE_SUCCESS)
        while True:
            (ret, cur, pen) = self.pipeline.get_state()
            if ret == gst.STATE_CHANGE_SUCCESS and cur == gst.STATE_NULL:
                break

##     def testProbedLink(self):
##         self.pipeline.add(self.src)
##         pad = self.src.get_pad("src")
        
##         self.sink.connect_handoff(self._sink_handoff_cb)
##         self._handoffs = 0

##         # FIXME: adding a probe to the ghost pad does not work atm
##         # id = pad.add_buffer_probe(self._src_buffer_probe_cb)
##         realpad = pad.get_target()
##         self._probe_id = realpad.add_buffer_probe(self._src_buffer_probe_cb)

##         self._probed = False
        
##         while True:
##             (ret, cur, pen) = self.pipeline.get_state()
##             if ret == gst.STATE_CHANGE_SUCCESS and cur == gst.STATE_PLAYING:
##                 break

##         while not self._probed:
##             pass

##         while self._handoffs < 10:
##             pass

##         self.pipeline.set_state(gst.STATE_NULL)
##         while True:
##             (ret, cur, pen) = self.pipeline.get_state()
##             if ret == gst.STATE_CHANGE_SUCCESS and cur == gst.STATE_NULL:
##                 break

    def _src_buffer_probe_cb(self, pad, buffer):
        gst.debug("received probe on pad %r" % pad)
        self._probed = True
        gst.debug('adding sink bin')
        self.pipeline.add(self.sink)
        # this seems to get rid of the warnings about pushing on an unactivated
        # pad
        gst.debug('setting sink state')
        
        # FIXME: attempt one: sync to current pending state of bin
        (res, cur, pen) = self.pipeline.get_state(timeout=0)
        target = pen
        if target == gst.STATE_VOID_PENDING:
            target = cur
        gst.debug("setting sink state to %r" % target)
        # FIXME: the following print can cause a lock-up; why ?
        # print target
        # if we don't set async, it will possibly end up in PAUSED
        self.sink.set_state(target)
        
        gst.debug('linking')
        self.src.link(self.sink)
        gst.debug('removing buffer probe id %r' % self._probe_id)
        pad.remove_buffer_probe(self._probe_id)
        self._probe_id = None
        gst.debug('done')

    def _sink_handoff_cb(self, sink, buffer, pad):
        gst.debug('received handoff on pad %r' % pad)
        self._handoffs += 1

class TargetTest(TestCase):
    def test_target(self):
        src = gst.Pad("src", gst.PAD_SRC)

        ghost = gst.GhostPad("ghost_src", src)
        self.failUnless(ghost.get_target() is src)

        ghost.set_target(None)
        self.failUnless(ghost.get_target() is None)

        ghost.set_target(src)
        self.failUnless(ghost.get_target() is src)

if __name__ == "__main__":
    unittest.main()