【问题标题】:Gstreamer C Code is failed with streaming stopped, reason not-negotiated (-4)Gstreamer C 代码因流停止而失败,原因未协商 (-4)
【发布时间】:2019-09-28 04:02:59
【问题描述】:

我正在学习 Gstreamer,无论我通过 Gstreamer 工具获得了什么,我都在尝试使用 C 语言在 gstreamer 应用程序中实现相同的目标。

以下命令成功流式传输 mp4 视频文件: gst-launch-1.0.exe -v filesrc location=file.mp4 ! qtdemux ! h264解析! avdec_h264 !视频转换!自动视频接收器

我对 C 代码进行了同样的尝试,还使用“添加了填充”的元素信号来创建填充并链接到下一个元素,即解析器 (h264parser)。

所以,它因流停止而失败,原因未协商。

完整输出: 现在播放:file.mp4 跑步... 为 demux 创建了一个新的 pad video_0 元素 demux 将链接到解析器 错误:内部数据流错误。 调试信息:../gst/isomp4/qtdemux.c(6607): gst_qtdemux_loop (): /GstPipeline:video-play/GstQTDemux:demux: 流媒体停止,原因未协商 (-4) 已返回,停止播放... 释放管道... 完全的。再见!

#include <gst/gst.h>
#include <stdlib.h>
#include <string.h>

#define INPUT_FILE "file.mp4"

static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data)
{
    GMainLoop *loop = (GMainLoop *)data;

    switch (GST_MESSAGE_TYPE(msg)) {
        gchar  *debug;
        GError *error;

    case GST_MESSAGE_EOS:
        g_print("End of stream\n");
        g_main_loop_quit(loop);
        break;

    case GST_MESSAGE_ERROR:

        gst_message_parse_error(msg, &error, &debug);
        g_free(debug);

        g_printerr("Error: %s\n", error->message);
        g_printerr("Debug Information: %s\n", debug);
        g_error_free(error);

        g_main_loop_quit(loop);
        break;
    default:
        break;
    }

    return TRUE;
}


static void on_pad_added(GstElement *element, GstPad *pad, gpointer data)
{
    gchar *name;
    GstElement *parse = (GstElement *)data;

    name = gst_pad_get_name(pad);
    g_print("A new pad %s was created for %s\n", name, gst_element_get_name(element));
    g_free(name);

    g_print("element %s will be linked to %s\n",
        gst_element_get_name(element),
        gst_element_get_name(parse));

    gst_element_link(element, parse);
}

int main(int argc, char *argv[])
{
    GMainLoop *loop;
    GstElement *pipeline, *source, *demux, *parser, *decoder, *sink, *fpssink;
    GstBus *bus;
    guint bus_watch_id;

    const gchar *input_file = INPUT_FILE;

    /* Initialization */
    gst_init(&argc, &argv);
    loop = g_main_loop_new(NULL, FALSE);

    /* Create gstreamer elements */
    pipeline = gst_pipeline_new("video-play");
    source = gst_element_factory_make("filesrc", "file-source");
    demux = gst_element_factory_make("qtdemux", "demux");
    parser = gst_element_factory_make("h264parse", "h264-parser");
    decoder = gst_element_factory_make("avdec_h264", "decoder");
    sink = gst_element_factory_make("d3dvideosink", "video-output");

    if (!pipeline || !source || !demux || !parser || !decoder || !sink) {
        g_printerr("One element could not be created. Exiting.\n");
        return -1;
    }

    /* Set input video file for source element */
    g_object_set(G_OBJECT(source), "location", input_file, NULL);

    /* we add a message handler */
    bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
    bus_watch_id = gst_bus_add_watch(bus, bus_call, loop);
    gst_object_unref(bus);

    /* Add all elements into the pipeline */
    /* pipeline---[ filesrc + qtdemux + h264parse + avdec_h264 + d3dvideosink ] */
    gst_bin_add_many(GST_BIN(pipeline), source, demux, parser, decoder, sink, NULL);

    /* Link the elements filesrc->demux together */

    if (gst_element_link(source, demux) != TRUE) {
        g_printerr("Element source->demux could not be linked.\n");
        gst_object_unref(pipeline);
        return -1;
    }
    /* h264parse -> avdec_h264 -> d3dvideosink */

    if (gst_element_link_many(parser, decoder, sink, NULL) != TRUE) {
            g_printerr("Many Elements could not be linked.\n");
            gst_object_unref(pipeline);
            return -1;
    }

    g_signal_connect(demux, "pad-added", G_CALLBACK(on_pad_added), parser);

    /* Set the pipeline to "playing" state */
    g_print("Now playing: %s\n", input_file);
    if (gst_element_set_state(pipeline,
        GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
        g_printerr("Unable to set the pipeline to the playing state.\n");
        gst_object_unref(pipeline);
        return -1;
    }

    g_print("Running...\n");
    g_main_loop_run(loop);


    /* Free resources and change state to NULL */
    gst_object_unref(bus);
    g_print("Returned, stopping playback...\n");
    gst_element_set_state(pipeline, GST_STATE_NULL);
    g_print("Freeing pipeline...\n");
    gst_object_unref(GST_OBJECT(pipeline));
    g_print("Completed. Goodbye!\n");
    return 0;
}

请让我知道如何将这些 pad 链接到 h264parser 元素以流式传输视频文件。如果可能,请说明这些 pad 在 Gstreamer 工具和应用程序中是如何工作的

【问题讨论】:

  • 添加队列元素后问题解决,在 g_signal_connect() 中将 demux 链接到队列而不是 demux 到解析器。谁能解释一下?

标签: c video gstreamer gstreamer-1.0


【解决方案1】:

以下命令成功流式传输 mp4 视频文件: gst-launch-1.0.exe -v filesrc location=file.mp4 ! qtdemux ! h264解析 ! avdec_h264 !视频转换!自动视频接收器

理想情况下,您的管道应该是:

gst-launch-1.0.exe -v filesrc location=file.mp4 ! qtdemux 名称=d d.video_0 !队列 ! h264解析! avdec_h264 !视频转换! 自动视频接收器

如果您检查 qtdemux (gst-inspect-1.0 qtdemux),您会注意到 SINK 焊盘具有以下上限:

能力: 视频/快速时间 视频/mj2 音频/x-m4a 应用程序/x-3gp

如果您检查 h264parse (gst-inspect-1.0 h264parse) ,您会注意到 SRC 垫具有以下上限:

SRC 模板:'src' 可用性:总是 能力: 视频/x-h264 解析:真 流格式:{(字符串)avc,(字符串)avc3,(字符串)字节流} 对齐:{ (string)au, (string)nal }

当您尝试将 Qtdemux 的 sink pad 链接到 h264parse 的 src pad 时,您可能需要收集视频 cap 以与 h264parse 连接。

我已使用以下代码将 qtdemux 与 h264parse 链接到“pad-added”信号中:

static void pad_added_handler(GstElement *src, GstPad *new_pad, gpointer *data) {
   GstPadLinkReturn ret;
   GstCaps *new_pad_caps = NULL;
   GstStructure *new_pad_struct = NULL;
   const gchar *new_pad_type = NULL;
   GstElement *h264parse = (GstElement *) data;


   /* Check the new pad's type */
   new_pad_caps = gst_pad_get_current_caps(new_pad);
   new_pad_struct = gst_caps_get_structure(new_pad_caps, 0);
   new_pad_type = gst_structure_get_name(new_pad_struct);

    if (g_str_has_prefix(new_pad_type, "video/x-h264")) {
          GstPad *sink_pad_video = gst_element_get_static_pad (h264parse, "sink");
          ret = gst_pad_link(new_pad, sink_pad_video);
     }
   }

注意: 您可能需要链接 caps 过滤器以过滤视频源的所需视频功能,或者只是尝试以下条件: if (g_str_has_prefix(new_pad_type, "video")) {}

但我不确定添加队列如何解决您的问题。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我刚刚编辑并修复了 shafeer 的答案。

    这是有效的代码:

    static void pad_added_handler(GstElement *src, GstPad *new_pad, gpointer *data) {
       GstPadLinkReturn ret;
       GstCaps *new_pad_caps = nullptr;
       GstStructure *new_pad_struct = nullptr;
       const gchar *new_pad_type = nullptr;
       GstElement *h264parse = (GstElement *) data;
    
       /* Check the new pad's type */
       new_pad_caps = gst_pad_get_current_caps(new_pad);
       new_pad_struct = gst_caps_get_structure(new_pad_caps, 0);
       new_pad_type = gst_structure_get_name(new_pad_struct);
    
        if (g_str_has_prefix(new_pad_type, "video/x-h264")) {
              GstPad *sink_pad_video = gst_element_get_static_pad (h264parse, "sink");
              ret = gst_pad_link(new_pad, sink_pad_video);
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-21
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多