GstMultifilesink post-messages callback

家住魔仙堡 提交于 2021-01-02 16:35:14

问题


I would like to know how to get a callback from the gstreamer multifilesink element using post-messages=TRUE property? In the code below my_bus_callback function is never called.

Multifilesink docs say: If the "post-messages" property is TRUE, it sends an application message named "GstMultiFileSink" after writing each buffer.

http://www.freedesktop.org/software/gstreamer-sdk/data/docs/2012.5/gst-plugins-good-plugins-0.10/gst-plugins-good-plugins-multifilesink.html#GstMultiFileSink--post-messages

#include <gst/gst.h>
#include <stdio.h>
#include <unistd.h>

// Compilation:
// gcc gst_messages.c -g3 -o gst_messages `pkg-config --cflags --libs gstreamer-0.10`

static GMainLoop *loop;

static gboolean
my_bus_callback (GstBus     *bus,
         GstMessage *message,
         gpointer    data)
{
  printf("Got %s message\n", GST_MESSAGE_TYPE_NAME (message));

  /* we want to be notified again the next time there is a message
   * on the bus, so returning TRUE (FALSE means we want to stop watching
   * for messages on the bus and our callback should not be called again)
   */
  return TRUE;
}


// http://pastebin.com/B3QixHCX
int main(int argc, char *argv[]) {
    GstBus *bus;
    guint bus_watch_id;

    gst_init(&argc, &argv);

    GstElement *pipeline = gst_pipeline_new("looper-pipeline");
    GstElement *v4l2src = gst_element_factory_make("v4l2src", "src");
    GstElement *x264enc = gst_element_factory_make("x264enc", "encoder");
    GstElement *mpegtsmux = gst_element_factory_make("mpegtsmux", "mpegtsmux");
    GstElement *multifilesink = gst_element_factory_make("multifilesink", "multifilesink");


    g_object_set(multifilesink, "next-file", 2, NULL);
    g_object_set(multifilesink, "location", "%05d.ts", NULL);
    g_object_set(multifilesink, "post-messages", TRUE, NULL);

    gst_bin_add(GST_BIN (pipeline), v4l2src);
    gst_bin_add(GST_BIN (pipeline), x264enc);
    gst_bin_add(GST_BIN (pipeline), mpegtsmux);
    gst_bin_add(GST_BIN (pipeline), multifilesink);

    gboolean success;
    success = gst_element_link(v4l2src, x264enc);
    if (!success) {
        g_printerr("Elements could not be linked 1.\n");
    }
    success = gst_element_link(x264enc, mpegtsmux);
    if (!success) {
        g_printerr("Elements could not be linked 2.\n");
    }
    success = gst_element_link(mpegtsmux, multifilesink);
    if (!success) {
        g_printerr("Elements could not be linked 3.\n");
    }

    bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
    gst_bus_add_signal_watch (bus);
    g_signal_connect (bus, "GstMultiFileSink", G_CALLBACK (my_bus_callback), NULL);

    printf("Running\n");
    gst_element_set_state (pipeline, GST_STATE_PLAYING);
    sleep(35);
    printf("STOPPING\n");
    GstMessage *msg;

    gst_element_send_event(v4l2src, gst_event_new_eos()); // shut down pipeline

    /* Wait until error or EOS */
    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);
    printf("SETTING PIPELINE TO NULL\n");
    gst_element_set_state(pipeline, GST_STATE_NULL);
    gst_object_unref(pipeline);
}

回答1:


OK I got messages coming in with two changes to your code. On your signal use this:

g_signal_connect (bus, "message::element", G_CALLBACK (my_bus_callback), NULL);

The documentation doesn't mention it very clearly, but apparently there are different signal names, like message::eos. You're looking for element messages.

http://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/chapter-bus.html

http://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/section-bus-message-types.html

The next thing you need to change is to add a GMainLoop and call to g_main_loop_run. This is apparently what drives all the message passing.

gboolean end_my_pipeline_somehow(gpointer data) {
  //end the pipeline
  return TRUE;
}

g_timeout_add_seconds(35, end_my_pipeline_somehow, pipeline);

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

Update

message::element is a valid signal; it allows the bus to listen for messages posted by elements.



来源:https://stackoverflow.com/questions/34594273/gstmultifilesink-post-messages-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!