9

我有一个 Qt 应用程序,它在单独的线程中执行与 GStreamer 相关的工作。虽然我认为我已经遵循了设置信号回调的规则,但我指定的回调函数似乎没有被调用。

回调函数如下,它试图做的只是将一些内容记录到控制台以进行调试:

static gboolean Cb(GstBus *bus, GstMessage *msg, gpointer data)
{
    std::cout << "g_sig, bus = " << (void*)bus
              << ", msg = "      << (void*)msg
              << ", data = "     << (void*)data
              << std::endl;
    return TRUE;
}

我用来启动和监视流(来自 IP 摄像机的实时 RTSP/H.264 馈送)的顺序是:

GstElement *playBin = gst_parse_launch("<gstreamer pipeline>");
GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(playBin));
gst_bus_add_signal_watch(bus);
g_signal_connect(bus, "message::state-changed", (GCallback)Cb, NULL);
gst_element_set_state(playBin, GST_STATE_PLAYING);

GMainLoop *mainLoop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(mainLoop);

现在流实际上正在播放(视频出现)所以我假设那里没有问题。但是,我希望在管道开始播放时发布状态更改消息。这似乎没有发生,因为我没有看到来自Cb().

即使我也捕捉到message::eos,message::errormessage::element信号,我也没有得到任何输出。

我不确定这是否会成为问题,但以防万一,上面的代码稍微简化了。实际上有两个流正在播放,所以上面的第一个代码片段发生了两次,每个 playbin 一次(每个 playbin 的序列都是准确的,我只是看到没有必要使代码复杂化)。

然后创建并运行主循环。

如前所述,我没有看到回调函数的输出,那么我的消息去哪里了?

附录:对于它的价值,我也尝试了gst_bus_add_watch捕获所有消息而不是特定信号的方法,但仍然没有任何显示。我还应该提到,作为一个 Qt 应用程序,我gtk_init在代码中没有 - 我只是gst_init从主线程调用。

4

1 回答 1

6

我对 gstreamer 的了解有限,很久以前就搞砸了。

我做了一个小测试,这似乎有效:

#include <stdio.h>
#include <gstreamer-1.0/gst/gst.h>

static gboolean Cb(GstBus *bus, GstMessage *msg, gpointer data)
{
        printf("g_sig, bus = %x, msg = %s, data = %x\n",
          bus,
          gst_message_type_get_name(GST_MESSAGE_TYPE(msg)),
          data);

        return TRUE;
}

int main(int argc, char *argv[]) {
        GstElement *pipeline;
        GstBus *bus;
        GstMessage *msg;

        /* Initialize GStreamer */
        gst_init (&argc, &argv);

        /* Build the pipeline */
        pipeline = gst_parse_launch ("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);

        /* Wait until error or EOS */
        bus = gst_element_get_bus (pipeline);

        /* use the sync signal handler to link elements while the pipeline is still
         * doing the state change */
        gst_bus_set_sync_handler (bus, gst_bus_sync_signal_handler, pipeline, NULL);
        g_object_connect (bus, "signal::sync-message::state-changed", G_CALLBACK (Cb), pipeline, NULL);

        /* Start playing */
        gst_element_set_state (pipeline, GST_STATE_PLAYING);

        msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);

        /* Free resources */
        if (msg != NULL)
                gst_message_unref (msg);

        gst_object_unref (bus);
        gst_element_set_state (pipeline, GST_STATE_NULL);
        gst_object_unref (pipeline);

        return 0;
}

但是虽然message::state-changed

公交车上贴了一条消息。该信号是从添加到主循环的 GSource 发出的。只有在主循环运行时才会发出此信号。

signal::sync-message::state-changed反而:

公交车上贴了一条消息。该信号是从发布消息的线程发出的,因此必须小心锁定。

所以同步消息也在主循环之外发出。

两个信号的文档

于 2016-11-29T22:25:00.407 回答