无法链接 Gstreamer 元素

Gstreamer Elements could not be linked

我是 GStreamer 的新手,我遵循 Basic tutorial 2 on GStreamer website 并尝试使其与本地 mp4 文件一起使用。 我的问题是我不能 link "uridecodebin" 和 "autovideosink",这是我的相关代码:

  GstElement *pipeline, *source, *sink;
  GstBus *bus;
  GstMessage *msg;
  GstStateChangeReturn ret;

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

  /* Create the elements */
  source = gst_element_factory_make ("uridecodebin", "source");
  sink = gst_element_factory_make ("autovideosink", "sink");

  /* Create the empty pipeline */
  pipeline = gst_pipeline_new ("pipeline");

  if (!pipeline || !source || !sink) {
      g_printerr ("Not all elements could be created.\n");
      return -1;
  }

  /* Build the pipeline */
  gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
  if (gst_element_link (source, sink) != TRUE) {
      g_printerr ("Elements could not be linked.\n");
      gst_object_unref (pipeline);
      return -1;
  }

使用 gst_element_link (source, sink) 时总是 return 错误,但如果我只是使用 gst-launch-1.0 uridecodebin uri=file://MyPathToVideo/test.mp4 ! autovideosink 命令,效果很好,我做错了什么?

非常感谢。

尝试使用 gst_parse_launch() 并为其提供管道。这样更短。而且我相信它会处理您案例中的一些特殊情况。

您的方法不起作用的主要问题是 uridecodebin 没有暴露任何打击垫,因为在那个时间点它对您的 MP4 文件一无所知。所以它可以包含音频、视频,或者两者兼而有之。正确的做法是实现延迟链接。

因此,您可以在 uridecodebin 上实现 pad-added 信号,而不是直接链接它: https://gstreamer.freedesktop.org/documentation/gstreamer/gstelement.html?gi-language=c#GstElement::pad-added

然后在断开元素的情况下启动管道。

uridecodebin 扫描您的媒体文件并显示可以链接的打击垫时,将触发此 pad-added 信号。如果是您的视频板,您可以将其连接到 autovideosink.

gst_parse_launch() 如果我没记错的话会自动为您执行此操作(至少那是 gst-lauch-1.0 正在做的 - 不确定该特定功能是否已移至 API)。

P.S。你抢先了。教程 2 没有使用 uridecodebin,而是使用了更多基本元素。教程 3 将介绍动态管道。