File: Elements_basics.py.md

package info (click to toggle)
pitivi 2023.03-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,468 kB
  • sloc: python: 33,616; ansic: 104; sh: 82; makefile: 6
file content (107 lines) | stat: -rw-r--r-- 3,284 bytes parent folder | download | duplicates (5)
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
# Elements basics.py

    #!/usr/bin/env python

    """
    gst.Element basics, this is a modified script of Brandon Lewis
    """

    # PiTiVi does this for you, but in any stand-alone gstreamer script you need
    # to do this before anything else
    import gobject
    gobject.threads_init()

    # import gst and gtk
    import gst
    import gtk


    class NewElement(gst.Element):
        """ A basic, buffer forwarding gstreamer element """

        #here we register our plugin details
        __gstdetails__ = (
            "NewElement plugin",
            "newelement.py",
            "gst.Element, that passes a buffer from source to sink (a filter)",
            "Stephen Griffiths <scgmk5@gmail.com>")

        #source pad (template): we send buffers forward through here
        _srctemplate = gst.PadTemplate ('src',
            gst.PAD_SRC,
            gst.PAD_ALWAYS,
            gst.caps_new_any())

        #sink pad (template): we receive buffers from our sink pad
        _sinktemplate = gst.PadTemplate ('sink',
            gst.PAD_SINK,
            gst.PAD_ALWAYS,
            gst.caps_new_any())

        #register our pad templates
        __gsttemplates__ = (_srctemplate, _sinktemplate)

        def __init__(self, *args, **kwargs):
            #initialise parent class
            gst.Element.__init__(self, *args, **kwargs)

            #source pad, outgoing data
            self.srcpad = gst.Pad(self._srctemplate)

            #sink pad, incoming data
            self.sinkpad = gst.Pad(self._sinktemplate)
            self.sinkpad.set_setcaps_function(self._sink_setcaps)
            self.sinkpad.set_chain_function(self._sink_chain)

            #make pads available
            self.add_pad(self.srcpad)
            self.add_pad(self.sinkpad)

        def _sink_setcaps(self, pad, caps):
            #we negotiate our capabilities here, this function is called
            #as autovideosink accepts anything, we just say yes we can handle the
            #incoming data
            return True

        def _sink_chain(self, pad, buf):
            #this is where we do filtering
            #and then push a buffer to the next element, returning a value saying
            # it was either successful or not.
            return self.srcpad.push(buf)

    #here we register our class with glib, the c-based object system used by
    #gstreamer
    gobject.type_register(NewElement)





    ## this code creates the following pipeline, equivalent to
    ## gst-launch-0.10 videotestsrc ! videoscale ! ffmpegcolorspace !
    ### NewElement ! autovideosink

    # first create individual gstreamer elements

    source = gst.element_factory_make("videotestsrc")
    print "making new element"
    newElement = NewElement()
    print "made new element"
    vscale = gst.element_factory_make("videoscale")
    cspace = gst.element_factory_make("ffmpegcolorspace")
    vsink  = gst.element_factory_make("autovideosink")

    # create the pipeline

    p = gst.Pipeline()
    p.add(source, vscale, cspace, newElement,
        vsink)
    gst.element_link_many(source, vscale, cspace, newElement,
        vsink)
    # set pipeline to playback state

    p.set_state(gst.STATE_PLAYING)

    # start the main loop, pitivi does this already.

    gtk.main()