test_ghostpad.py 6.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# -*- 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

23
from common import gst, unittest, TestCase, pygobject_2_13
24 25 26 27 28 29 30 31 32 33

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")
34
        ghostpad = gst.GhostPad("src", pad)
35 36 37 38 39 40 41 42
        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")
43
        ghostpad = gst.GhostPad("sink", pad)
44
        self.add_pad(ghostpad)
45 46 47 48 49 50
        self.sink = sink

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

        
class PipeTest(TestCase):
    def setUp(self):
Edward Hervey's avatar
Edward Hervey committed
56
        gst.info("setUp")
57
        TestCase.setUp(self)
58 59
        self.pipeline = gst.Pipeline()
        self.assertEquals(self.pipeline.__gstrefcount__, 1)
60
        self.assertEquals(sys.getrefcount(self.pipeline), pygobject_2_13 and 2 or 3)
61 62 63 64 65 66

        self.src = SrcBin()
        self.src.prepare()
        self.sink = SinkBin()
        self.sink.prepare()
        self.assertEquals(self.src.__gstrefcount__, 1)
67
        self.assertEquals(sys.getrefcount(self.src), pygobject_2_13 and 2 or 3)
68
        self.assertEquals(self.sink.__gstrefcount__, 1)
69
        self.assertEquals(sys.getrefcount(self.sink), pygobject_2_13 and 2 or 3)
Edward Hervey's avatar
Edward Hervey committed
70
        gst.info("end of SetUp")
71 72

    def tearDown(self):
Edward Hervey's avatar
Edward Hervey committed
73
        gst.info("tearDown")
74
        self.assertTrue (self.pipeline.__gstrefcount__ >= 1 and self.pipeline.__gstrefcount__ <= 2)
75
        self.assertEquals(sys.getrefcount(self.pipeline), pygobject_2_13 and 2 or 3)
76
        self.assertEquals(self.src.__gstrefcount__, 2)
77
        self.assertEquals(sys.getrefcount(self.src), pygobject_2_13 and 2 or 3)
78 79 80 81 82 83 84 85
        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
86 87
        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)
88 89 90 91 92 93 94
        gst.debug('deleting src')
        del self.src
        self.gccollect()
        gst.debug('deleting sink')
        del self.sink
        self.gccollect()

95
        TestCase.tearDown(self)
96
        
97
    def testBinState(self):
98 99
        self.pipeline.add(self.src, self.sink)
        self.src.link(self.sink)
100 101
        self.sink.connect_handoff(self._sink_handoff_cb)
        self._handoffs = 0
102

103
        self.assertTrue(self.pipeline.set_state(gst.STATE_PLAYING) != gst.STATE_CHANGE_FAILURE)
104
        while True:
Edward Hervey's avatar
Edward Hervey committed
105
            (ret, cur, pen) = self.pipeline.get_state()
106 107 108
            if ret == gst.STATE_CHANGE_SUCCESS and cur == gst.STATE_PLAYING:
                break

109 110 111
        while self._handoffs < 10:
                pass

112
        self.assertEquals(self.pipeline.set_state(gst.STATE_NULL), gst.STATE_CHANGE_SUCCESS)
113
        while True:
Edward Hervey's avatar
Edward Hervey committed
114
            (ret, cur, pen) = self.pipeline.get_state()
115 116 117
            if ret == gst.STATE_CHANGE_SUCCESS and cur == gst.STATE_NULL:
                break

Edward Hervey's avatar
Edward Hervey committed
118 119 120
##     def testProbedLink(self):
##         self.pipeline.add(self.src)
##         pad = self.src.get_pad("src")
121
        
Edward Hervey's avatar
Edward Hervey committed
122 123
##         self.sink.connect_handoff(self._sink_handoff_cb)
##         self._handoffs = 0
124

Edward Hervey's avatar
Edward Hervey committed
125 126 127 128
##         # 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)
129

Edward Hervey's avatar
Edward Hervey committed
130
##         self._probed = False
131
        
Edward Hervey's avatar
Edward Hervey committed
132 133 134 135
##         while True:
##             (ret, cur, pen) = self.pipeline.get_state()
##             if ret == gst.STATE_CHANGE_SUCCESS and cur == gst.STATE_PLAYING:
##                 break
136

Edward Hervey's avatar
Edward Hervey committed
137 138
##         while not self._probed:
##             pass
139

Edward Hervey's avatar
Edward Hervey committed
140 141
##         while self._handoffs < 10:
##             pass
142

Edward Hervey's avatar
Edward Hervey committed
143 144 145 146 147
##         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
148

149 150 151 152 153 154 155
    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
156 157 158
        gst.debug('setting sink state')
        
        # FIXME: attempt one: sync to current pending state of bin
Edward Hervey's avatar
Edward Hervey committed
159
        (res, cur, pen) = self.pipeline.get_state(timeout=0)
160 161 162 163 164 165 166
        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
Edward Hervey's avatar
Edward Hervey committed
167
        self.sink.set_state(target)
168
        
169 170 171 172 173 174 175
        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')

Edward Hervey's avatar
Edward Hervey committed
176
    def _sink_handoff_cb(self, sink, buffer, pad):
177 178 179
        gst.debug('received handoff on pad %r' % pad)
        self._handoffs += 1

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