c - Gstreamer C 代码因流停止而失败,原因未协商 (-4)

标签 c video gstreamer gstreamer-1.0

我正在学习 Gstreamer 以及我通过 Gstreamer 工具取得的任何成就,我正在尝试使用 C 语言通过 gstreamer 应用程序实现相同的目标。

下面的命令成功地流式传输了一个 mp4 视频文件: gst-launch-1.0.exe -v filesrc location=file.mp4 ! qt解复用器! h264解析! avdec_h264 !视频转换!自动视频接收器

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

因此,流式传输停止失败,原因未协商。

完整输出: 正在播放:file.mp4 运行... 为 demux 创建了一个新的 pad video_0 element 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;
}

你们能告诉我如何将这些垫链接到 h264parser 元素以流式传输视频文件吗?如果可能,请解释这些垫在 Gstreamer 工具和应用程序中如何工作

最佳答案

Below command streamed a mp4 video file successfully: gst-launch-1.0.exe -v filesrc location=file.mp4 ! qtdemux ! h264parse ! avdec_h264 ! videoconvert ! autovideosink

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

gst-launch-1.0.exe -v filesrc location=file.mp4 ! qtdemux name=d d.video_0 ! queue ! h264parse ! avdec_h264 ! videoconvert ! autovideosink

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

能力: 视频/quicktime 视频/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 时,您可能必须收集视频上限才能与 h264parse 连接。

我使用下面的代码将 qtdemux 与“pad-added”信号中的 h264parse 链接起来:

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);
     }
   }

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

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

希望这对您有所帮助。

关于c - Gstreamer C 代码因流停止而失败,原因未协商 (-4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56074308/

相关文章:

linux - 有没有 gstreamer 插件可以扩展视频流?

ffmpeg - 通过将视频并排放置,将两个视频设备合并到第三个设备中

c - C语言中如何知道未知类型变量的数据类型?

在 C 中创建文本中的数字数组

ios - 在 CGPath 或 UIBezierPath 内播放视频

audio - 水平并排显示两个视频并导出为一个视频

python - 使用 Python OpenCV cv2.VideoCapture() 直接读取灰度视频帧

video - 查找 MJPEG 视频的特定帧是否比查找 h.264 文件更快?

c - Linux 内核模块 : Socket buffer (sk_buff->len) non-deterministic behaviour

c++ - 在 C++ 中访问 return 语句后执行函数中的代码