使用非静态消息处理程序获取gstreamer总线消息

问题描述 投票:0回答:1

我使用gstreamer创建了一个程序,它监听rtp数据包的不同端口(比如5)。

现在我创建了一个创建管道的类(比如说GstClass),并且有一个Callback函数来监听总线消息(我需要这个消息系统在一定的超时后关闭管道)。

main函数看起来像这样 - 使用2个对象创建2个线程,并在两个线程中调用GstFunc。第一个函数将侦听端口5000,第二个函数将侦听端口5008

int main() {

    char filepath1[ ]= "/home/rohan/Tornado1.raw";
    char filepath2[ ]= "/home/rohan/Tornado2.raw";
    unsigned int port1 = 5000;
    unsigned int port2 = 5008;

    GstClass GstObj1;
    GstClass GstObj2;

    boost::thread thrd1 { &GstClass::GstFunc, &GstObj1, filepath1, &port1 };
    boost::thread thrd2 { &GstClass::GstFunc, &GstObj2, filepath2, &port2 };

    thrd1.join();
    thrd2.join();

    return 0;
}

GstClass看起来像这样 -

class GstClass {
protected:
    //some other variables...
    GMainLoop *msLoop;

public:

    gboolean bus_call(GstBus *bus, GstMessage *message,
        gpointer data);

    void GstFunc(char *filepath, unsigned int *port);

};

有关详细的功能视图,请查看this example。用int main (int argc, char *argv[])替换函数void GstFunc(char *filepath, unsigned int *port)并进行适当的更改。

GstFunc看起来像

void GstFunc(char *filepath, unsigned int *port)
    GMainLoop *loop;
    GstElement *pipeline, *source, *conv, *sink;
    GstBus *bus;
    guint bus_watch_id;

    gst_init (NULL, NULL);

    loop = g_main_loop_new (NULL, FALSE);

    /* Create gstreamer elements */
    pipeline = gst_pipeline_new ("audio-player");
    source   = gst_element_factory_make ("autoaudiosrc",       "audiosource");
    conv     = gst_element_factory_make ("audioconvert",  "converter");
    sink     = gst_element_factory_make ("autoaudiosink", "audio-output");

    if (!pipeline || !source || !conv || !sink) {
          g_printerr ("One element could not be created. Exiting.\n");
          return -1;
    }
    /* we add a message handler */
    bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
    bus_watch_id = gst_bus_add_watch (bus, bus_call, NULL);
    gst_object_unref (bus);

    gst_bin_add_many (GST_BIN (pipeline), source, conv, sink, NULL);
    gst_element_link_many (GST_BIN (pipeline), source, conv, sink, NULL);

    g_main_loop_run (loop);

    gst_element_set_state (pipeline, GST_STATE_NULL);
    gst_object_unref (GST_OBJECT (pipeline));
    g_source_remove (bus_watch_id);
    g_main_loop_unref (loop);

    return 0;
}

现在我面临的困境是静态函数(例如)bus_call(...)

由于我在2个不同的线程中创建2个管道,这些线程正在监听2个不同的端口,因此我不能将此功能作为静态(在对象之间共享)。如何让这两条管道相互脱离?或者我怎样才能让这个静态bus_call(...)成为非静态的?

只是删除静态关键字没有帮助,并给出此错误

错误:无效使用非静态成员函数'gboolean GstClass :: bus_call(GstBus *,GstMessage *,gpointer)'

很少的Imp点

我已经提到this document说要使用总线,使用gst_bus_add_watch()将消息处理程序附加到管道总线

gst_bus_add_watch()中的GstClass::GstFunc()(回调bus_call)被映射到头文件gstbus.h,声明很简单

GST_API
guint gst_bus_add_watch(GstBus * bus, GstBusFunc func, gpointer user_data);

我最初的猜测是gst_bus_add_watch期望第二个参数是一个静态函数。我不知道为什么。这可以做什么?

***********************问题编辑2 ***********************

是否可以像bus_call一样在gboolean bus_call(GstBus *bus, GstMessage *message,gpointer data,**SOME POINTER TO THE OBJECT**)中添加一个参数?

这样,函数将保持静态,同时具有指向调用它的对象的指针,并对对象(例如该对象的关闭管道)进行操作。

c++ static gstreamer pipeline static-members
1个回答
2
投票

我认为你不能逃脱你想要的东西。回调GstBusFunc()的签名是指向函数的指针,而不是指向成员函数的指针。他们是different things。 (我也失败了std :: bind,fwiw)。

我做了一些与你描述的非常相似的东西,尽管不完全相同,但我采取了一种可能对你有所帮助的不同方法。您可以使用静态方法,但必须将指向管道类的指针传递给gst_bus_add_watch。在你的busCallback里面你取消引用指针然后离开!您可能还需要实现某种锁定方案。

class MyPipeline {

   GstElement *m_pipeline;

   public:

   MyPipeline(...);
   static void gboolean busCallback(GstBus *bus, GstMessage *msg, gpointer p);

}

MyPipeline::MyPipeline(...)
{
    // create pipeline...

    m_pipeline = ...;

    // bus callback, pass 'this' as arg for callback

    GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(m_pipeline));
    gst_bus_add_watch(bus, &MyPipeline::busCallback, this);
    gst_object_unref(bus);

    // ...
}

gboolean MyPipeline::busCallback(GstBus *, GstMessage *msg, gpointer p)
{

    // get lock if needed...

    // recover your class instance
    MyPipeline *myPipeline = (MyPipeline *)p;

    // do what you need to, free lock

    return TRUE;
}
© www.soinside.com 2019 - 2024. All rights reserved.