gst-launch-1.0 有效,无法在 .c 中重新创建

gst-launch-1.0 works, cannot recreate in .c

我正在从事一个项目,该项目将通过摄像机的运动检测触发事件。我有以下 gst-launch-1.0 命令可以正常工作:

gst-launch-1.0 rtspsrc location=rtsp://user:pass@10.10.40.200/live.sdp ! rtph264depay ! h264parse ! avdec_h264 ! video/x-raw ! videoconvert ! motioncells ! videoconvert ! xvimagesink

并尝试使用以下代码重新创建它:

#include <gst/gst.h>
#include <glib.h>
#include <glib/gprintf.h>

/*Structure to contain info so that we can pass it to callback*/
typedef struct _CustomData{
    GstElement *pipeline;
    GstElement *source;
    GstElement *depay;
    GstElement *parse;
    GstElement *decode;
    GstElement *capsf;
    GstElement *conv1;
    GstElement *mcells;
    GstElement *conv2;  
    GstElement *sink;
    GstElement *fsink;
    GstElement *fsource;
} CustomData;

int main(int argc, char *argv[]){
  CustomData data;
  GstBus *bus;
  GstMessage *msg;
  GstStateChangeReturn ret;
  gboolean terminate = FALSE;
  GstCaps *caps;

  /*init gstreamer*/
  gst_init(&argc, &argv);

  /*create elements*/
  data.source = gst_element_factory_make("rtspsrc","source");
  data.depay = gst_element_factory_make("rtph264depay","depay");
  data.parse = gst_element_factory_make("h264parse","parse");
  data.decode = gst_element_factory_make("avdec_h264","decode");
  data.capsf = gst_element_factory_make("capsfilter", "capsf");
  data.conv1 = gst_element_factory_make("videoconvert","conv1");
  data.mcells = gst_element_factory_make("motioncells","mcells");
  data.conv2 = gst_element_factory_make("videoconvert", "conv2");
  data.sink = gst_element_factory_make("xvimagesink", "sink");


  //create pipeline
  data.pipeline = gst_pipeline_new("mcells-pipeline");

  if(!data.pipeline || !data.source || !data.depay || !data.parse || !data.decode || !data.capsf || !data.conv1 || !data.mcells || !data.conv2 || !data.sink){
  g_printerr("Not all elements created\n");
  return -1;
  }  

  //set rtsp src
  g_object_set(G_OBJECT (data.source), "location", argv[1], NULL);

  //set capsf
  caps = gst_caps_from_string("video/x-raw");
  g_object_set(G_OBJECT(data.capsf), "caps", caps, NULL);

  //build the pipeline
  gst_bin_add_many(GST_BIN(data.pipeline), data.source, data.depay, data.parse, data.decode, data.capsf, data.conv1, data.mcells , data.conv2, data.sink, NULL);


  gst_element_link_many(data.source, data.depay, data.parse, data.decode, data.capsf,   data.conv1, data.mcells, data.conv2, data.sink, NULL);


  g_print ("Now playing\n");
  gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
  g_print ("Running...\n");

  bus = gst_element_get_bus(data.pipeline);
  while (!terminate){
  msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE,GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
  if (msg != NULL) {
  GError *err;
  gchar *debug_info;

  switch (GST_MESSAGE_TYPE (msg)) {
      case GST_MESSAGE_ERROR:
      gst_message_parse_error (msg, &err, &debug_info);
      g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
      g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
      g_clear_error (&err);
      g_free (debug_info);
      terminate = TRUE;
      break;
  case GST_MESSAGE_EOS:
      g_print ("End-Of-Stream reached.\n");
      terminate = TRUE;
      break;
  case GST_MESSAGE_STATE_CHANGED:
  /* We are only interested in state-changed messages from the pipeline */
      if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data.pipeline)) {
      GstState old_state, new_state, pending_state;
      gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
      g_print ("Pipeline state changed from %s to %s:\n",
      gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));
      }
      break;
  default:
  /* We should not reach here */
      g_printerr ("Unexpected message received.\n");
      break;
      }
  gst_message_unref (msg);
    }
  }

  gst_element_set_state (data.pipeline, GST_STATE_NULL);
  gst_object_unref (data.pipeline);
  return 0;
}

此代码导致错误:

Pipeline state changed from NULL to READY:
Pipeline state changed from READY to PAUSED:
Error received from element udpsrc1: Internal data flow error.
Debugging information: gstbasesrc.c(2865): gst_base_src_loop (): /GstPipeline:mcells-pipeline/GstRTSPSrc:source/GstUDPSrc:udpsrc1:
streaming task paused, reason not-linked (-1)

这里有没有人遇到过类似的错误,可以指出我哪里出错了?

谢谢。

rtspcsrc src-pads 并不总是可用的,因此在它发出 "pad-added" 事件之前,您不能 link 正确地添加其他元素,您可以在以下页面的 "Dynamic (or sometimes) pads" 部分:

https://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/chapter-pads.html