GStreamer播放RTSP视频流[通俗易懂]

GStreamer播放RTSP视频流[通俗易懂]本代码是使用GStreamer播放RTSP视频流,没有使用playbin,而是自己构建pipeline,经测试可以正常播放视频。代码如下:#include<gst/gst.h>/*Structuretocontainallourinformation,sowecanpassittocallbacks*/typedefstruct_CustomData{GstElement*pipeline;…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

        本代码是使用GStreamer播放RTSP视频流,没有使用playbin,而是自己构建pipeline,经测试可以正常播放视频。

        代码如下:

#include <gst/gst.h>

/* Structure to contain all our information, so we can pass it to callbacks */
typedef struct _CustomData {
    GstElement *pipeline;
    GstElement *source;
    GstElement *depay;
    GstElement *parse;
    GstElement *avdec;
    GstElement *convert;
    GstElement *resample;
    GstElement *sink;
} CustomData;

/* Handler for the pad-added signal */
static void pad_added_handler (GstElement *src, GstPad *pad, CustomData *data);

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

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

    /* Create the elements */
    data.source = gst_element_factory_make ("rtspsrc", "source");
    g_object_set (G_OBJECT (data.source), "latency", 2000, NULL);
    data.depay = gst_element_factory_make ("rtph264depay", "depay");
    data.parse = gst_element_factory_make ("h264parse", "parse");
    data.avdec = gst_element_factory_make ("avdec_h264", "avdec");
    data.convert = gst_element_factory_make ("videoconvert", "convert");
    // data.resample = gst_element_factory_make ("audioresample", "resample");
    data.sink = gst_element_factory_make ("autovideosink", "sink");

    // g_object_set (G_OBJECT (data.sink), "sync", FALSE, NULL);
 
    /* Set the URI to play */
    g_object_set (data.source, "location", "rtsp://localhost:9999/path", NULL);

    /* Connect to the pad-added signal */
    g_signal_connect (data.source, "pad-added", G_CALLBACK (pad_added_handler), &data);

    /* Create the empty pipeline */
    data.pipeline = gst_pipeline_new ("test-pipeline");

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

    /* Build the pipeline. Note that we are NOT linking the source at this
    * point. We will do it later. */
    gst_bin_add_many (GST_BIN (data.pipeline), data.source, data.depay, data.parse, data.avdec, data.convert, data.sink, NULL);
    if (!gst_element_link_many (data.depay, data.parse, data.avdec, data.convert, data.sink, NULL)) {
        g_printerr ("Elements could not be linked.\n");
        gst_object_unref (data.pipeline);
        return -1;
    }

    /* Start playing */
    ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
    if (ret == GST_STATE_CHANGE_FAILURE) {
        g_printerr ("Unable to set the pipeline to the playing state.\n");
        gst_object_unref (data.pipeline);
        return -1;
    }

    /* Listen to the bus */
    bus = gst_element_get_bus (data.pipeline);
    do {
        msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
            GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS);

        /* Parse message */
        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);
        }
    } while (!terminate);

    /* Free resources */
    gst_object_unref (bus);
    gst_element_set_state (data.pipeline, GST_STATE_NULL);
    gst_object_unref (data.pipeline);
    return 0;
}

/* This function will be called by the pad-added signal */
static void pad_added_handler (GstElement *src, GstPad *new_pad, CustomData *data) {
    GstPad *sink_pad = gst_element_get_static_pad (data->depay, "sink");
    GstPadLinkReturn ret;
    GstCaps *new_pad_caps = NULL;
    GstStructure *new_pad_struct = NULL;
    const gchar *new_pad_type = NULL;

    g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (new_pad), GST_ELEMENT_NAME (src));

    /* If our converter is already linked, we have nothing to do here */
    if (gst_pad_is_linked (sink_pad)) {
        g_print ("We are already linked. Ignoring.\n");
        goto exit;
    }

    /* 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);

    /* Attempt the link */
    ret = gst_pad_link (new_pad, sink_pad);
    if (GST_PAD_LINK_FAILED (ret)) {
        g_print ("Type is '%s' but link failed.\n", new_pad_type);
    } else {
        g_print ("Link succeeded (type '%s').\n", new_pad_type);
    }

exit:
    /* Unreference the new pad's caps, if we got them */
    if (new_pad_caps != NULL)
        gst_caps_unref (new_pad_caps);

    /* Unreference the sink pad */
    gst_object_unref (sink_pad);
}

编译命令:

gcc rtspplay.c `pkg-config --cflags --libs gstreamer-1.0`

RTSP地址换成自己的即可,上述代码只是简单展示如何使用pipeline播放RTSP视频。可以根据自己实际需求进行扩展,实现更加丰富的功能。

参考:

https://gstreamer.freedesktop.org/documentation/tutorials/basic/dynamic-pipelines.html?gi-language=c

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/180988.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • https://sweetalert2.github.io/

    https://sweetalert2.github.io/

    2022年3月13日
    77
  • 第十七章《redis主从复制》

    第十七章《redis主从复制》

    2021年5月29日
    98
  • java activity工作流[通俗易懂]

    java activity工作流[通俗易懂]javaactivity工作流参考资料:1.https://blog.csdn.net/jiangyu1013/article/details/732509022.https://blog.csdn.net/xnf1991/article/details/52610277—–这个比较详细3.https://www.cnblogs.com/shyroke/p/7…

    2022年6月6日
    63
  • Redis命令——集合(Set)[通俗易懂]

    Redis命令——集合(Set)[通俗易懂]Redis命令——集合(Set)

    2022年4月20日
    45
  • Python 逐行读取txt 文件并生成列表

    前言开始1.载入文件2.读取数据流3.数据处理4.关闭文件前言我们在编写一些自动化脚本的时候,为了方便,经常需要以txt文件作为数据输入,今天就跟大家讨论一下如何对txt文件进行读取并生成对应的列表等程序可操作的数据载体。开始1.载入文件这步就大家比较熟悉,文件操作中最基本的了。因为我们只需要读取文件,并不需要写入文件,所以在这里指定mode=”r”为只读模式(默认)。f=open(“C:/foo.txt”,”r”,encoding=’utf-8′)此时就有了这.

    2022年4月8日
    1.5K
  • 电脑磁盘未知没有初始化_win7怎么进去计算机管理

    电脑磁盘未知没有初始化_win7怎么进去计算机管理win7系统想必大家都非常熟悉吧,然而有时候可能会碰到win7系统电脑新增的硬盘没有初始化的情况,想必大家都遇到过win7系统电脑新增的硬盘没有初始化的情况吧,那么应该怎么处理win7系统电脑新增的硬盘没有初始化呢?我们依照  1、当正常增加新硬盘后,登录系统,打开磁盘管理,系统会自动打开【磁盘初始化和转换向导】,单击“下一步”; 2、正确选择要初始化的磁盘,单击“下一步”;这样的步骤就行了;下…

    2022年9月21日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号