interfacing gobject with C++

前端 未结 2 2098
野性不改
野性不改 2021-01-26 04:44

I’m trying to create a custom audio sink plugin for gstreamer using the Gst::AudioSink as a base class. For me this involves multiple learning curves as I’m new to gstreamer, gs

2条回答
  •  盖世英雄少女心
    2021-01-26 05:14

    It turns out that much of my troubles were caused by cutting and pasting this into MyAudioSink class:

    static GType get_base_type()
        {
           return Element::get_base_type();
        }
    

    This had the effect of telling gobject that my class is based on gstElement which was wrong. I thought it was some innocent cast like incantation. This shows the perils of cut and paste but more than that the perils of coding blindly. I was also guilty of oversimplifying the sample code I pasted here such that no-one my question doesn't show the problem.

    That fixes my problem but does not answer my question. I will try to summarise that below.

    "What is the equivalent for linking to the C++ virtual function hierarchy?"

    To create a wrapper to a gobject class the normal process is to use glibmmproc. The wrapper is defined by files with extension .hg and .ccg from which the C++ interface and a gobject wrapper are generated.

    For example to wrap a gobject classs foo you might create Foo.hg and Foo.ccg. glibmmproc would then generate Foo.h and Foo.cc. Foo.cc includes most of your definition of the Foo class but with an additional gobject wrapper Foo_class.

    Foo_class is a gobject class which wraps gobject virtual functions (vfunc_callbacks) and forwards them to Foo allowing derived classes of Foo to use C++ inheritance and C++ virtual functions. The boilerplate is hidden and a C++ developer need for the most part only worry about the C++ interface provided by Foo.h

    One way to understand the internals is to build gstreamermm from source and study the code generated by glibmmproc. For my case this would be: gstreamermm/audiosink.cc & gstreamermm/audiosink.h generated from src/audiosink.ccg and src/audiosink.hg

    So how does the derived C++ class register itself?

    • Gst::ElementFactory::register_element() - registers the class with gstreamer
    • Gst::register_mm_type - records the inheritance relationship

    See your local /usr/include/gstreamermm-1.0/gstreamermm/register.h for the implementation

    Glib::ObjectBase(typeid (MyAudioSink)) is not required in my case as I am not using multiple inheritance. However it is critical in other applications which do. See for example Implementing a custom gtkmm treemodel

提交回复
热议问题