How to write a GStreamer plugin in Cython

I want a prototype of a new type of sound filter. I am currently using GStreamer to return the buffer of my sound, and Cython is applying a filter to it. Then I send the result back to GStreamer.

Everything is wrapped in python core code, GStreamer images are executed using pygst . Using Cython for a prototype is excellent because it is automatically recompiled when the code runs and is very efficient.

However, I believe that it would be even better if I could use my Cython files to create a native GStreamer plugin (written in C).

Do you have an idea on how this can be achieved?

+6
source share
2 answers

Cython is primarily intended to create Python extension modules and is not really intended to support other plugin APIs. However, if you are ready to manually configure the output, you can get something reasonable.

For example, you can manually write a small C-stub to initialize your module as a gstreamer plugin:

#include "Python.h" #include "gst_plugin.h" static gboolean plugin_init (GstPlugin *plugin) { // initialize the extension module #if PY_MAJOR_VERSION < 3 initgstreamer(); #else PyInit_gstreamer(); #endif // call into function exported from cython module return register_plugin(plugin); } GST_PLUGIN_DEFINE ( GST_VERSION_MAJOR, GST_VERSION_MINOR, my_filter, "My filter plugin", plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/" ) 

Then you can export this register_plugin function from your cython module:

 cdef public int register_plugin(void *plugin): # use the gstreamer API in here to register your plugin 

However, this is not the whole story. For this to work, you need to somehow convince gstreamer to load libpython into your process, since cython depends on it even for initialization. You will probably need to initialize the Python interpreter a bit before your code will work as you wish. You will need to define cython stubs for all the gstreamer plugin registration APIs that you want to use. And if someone else tried to do the same in the same gstreamer process, it would probably all fall apart.

So with all that said, an easier way might be to make a gstreamer plugin that connects to Python code and then use that plugin to access your cython module. This way, the python nesting is explicit, and you can correctly initialize the Python interpreter before loading the code. Such a plugin can be useful for other people who are trying to carry out similar projects with yours, whether using cython or the ctypes module.

Such a plug-in would actually be inverted PyGST: it would load Python as a library in GStreamer, and not load GStreamer as a library in Python. There is probably some PyGST code that you could reuse for this purpose, but you still have a limitation that each process can contain only one Python, so everyone must use to use multiple modules using this mechanism the same Python interpreter, several applications are loaded into the mod_python module for the Apache web server.

+5
source

Cython creates Python extension modules that must be initialized in the Python context to be useful. I think that what you are trying to accomplish will not be possible unless you work with the gstreamer developers to first add support for the Python module in gstreamer.

+1
source

All Articles