python: Add a Gst.init_python function to be called from plugins
[platform/upstream/gstreamer.git] / subprojects / gst-python / examples / plugins / python / sinkelement.py
1 #!/usr/bin/env python
2 # -*- Mode: Python -*-
3 # vi:si:et:sw=4:sts=4:ts=4
4
5 # sinkelement.py
6 # (c) 2005 Edward Hervey <edward@fluendo.com>
7 # (c) 2007 Jan Schmidt <jan@fluendo.com>
8 # Licensed under LGPL
9 #
10 # Small test application to show how to write a sink element
11 # in 20 lines in python and place into the gstreamer registry
12 # so it can be autoplugged or used from parse_launch.
13 #
14 # You can run the example from the source doing from gst-python/:
15 #
16 #  $ export GST_PLUGIN_PATH=$GST_PLUGIN_PATH:$PWD/plugin:$PWD/examples/plugins
17 #  $ GST_DEBUG=python:4 gst-launch-1.0 fakesrc num-buffers=10 ! mysink
18 #
19 # Since this implements the `mypysink://` protocol you can also do:
20 #
21 #  $ gst-launch-1.0 videotestsrc ! pymysink://
22
23 from gi.repository import Gst, GObject, GstBase
24 Gst.init_python()
25
26 #
27 # Simple Sink element created entirely in python
28 #
29 class MySink(GstBase.BaseSink, Gst.URIHandler):
30     __gstmetadata__ = ('CustomSink','Sink', \
31                       'Custom test sink element', 'Edward Hervey')
32
33     __gsttemplates__ = Gst.PadTemplate.new("sink",
34                                            Gst.PadDirection.SINK,
35                                            Gst.PadPresence.ALWAYS,
36                                            Gst.Caps.new_any())
37
38     __protocols__ = ("pymysink",)
39     __uritype__ = Gst.URIType.SINK
40
41     def __init__(self):
42         super().__init__()
43         # Working around https://gitlab.gnome.org/GNOME/pygobject/-/merge_requests/129
44         self.__dontkillme = self
45
46     def do_render(self, buffer):
47         Gst.info("timestamp(buffer):%s" % (Gst.TIME_ARGS(buffer.pts)))
48         return Gst.FlowReturn.OK
49
50     def do_get_uri(self, uri):
51         return "pymysink://"
52
53     def do_set_uri(self, uri):
54         return True
55
56 GObject.type_register(MySink)
57 __gstelementfactory__ = ("mysink", Gst.Rank.NONE, MySink)