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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • PyCharm激活码永久有效PyCharm2019.3.5激活码教程-持续更新,一步到位

    PyCharm激活码永久有效PyCharm2019.3.5激活码教程-持续更新,一步到位PyCharm激活码永久有效2019.3.5激活码教程-Windows版永久激活-持续更新,Idea激活码2019.3.5成功激活

    2022年6月19日
    76
  • windowsform和wpf(winform和wpf我选哪个)

    WPF开发于WinForm之后,从技术发展的角度,WPF比WinForm先进是不容置疑的。我觉得WPF相比于WinForm有下面的一些较好的特性:解决WindowHandle问题在WindowsGDI或WinForm开发中复杂的GUI应用程序,会使用的大量的控件,如Grid等。而每个控件或Gridcell都是一个小窗口,会使用一个Windowhandle,尽管控件厂商提供了很多优化…

    2022年4月12日
    36
  • Vs2013 简单定制安装

    Vs2013 简单定制安装

    2022年3月5日
    31
  • moxa串口服务器配置说明(moxa串口驱动)

    串口服务器简介串口服务器提供串口转网络功能,能够将RS-232/485/422串口转换成TCP/IP网络接口,实现RS-232/485/422串口与TCP/IP网络接口的数据双向透明传输。使得串口设备能够立即具备TCP/IP网络接口功能,连接网络进行数据通信,极大的扩展串口设备的通信距离。MOXA串口服务器的工作方式1.TCP/UDP通讯模式:该模式下,串口服务器成对的使用,一个作为server端…

    2022年4月18日
    976
  • 编译CTK「建议收藏」

    编译CTK「建议收藏」 使用ctkPluginFramework作为插件系统框架的确有着众多开发上的优势。最近收到一些站内信,大家都想使用ctkPluginFramework但是不知道如何编译,这篇教程就来讲一讲ctkPluginFramework插件系统在Windows下的编译过程。准备条件:https://wenku.baidu.com/view/83ef9e1be97101f69e3143323968011ca3…

    2022年5月3日
    73
  • 维度建模——数据仓库初步[通俗易懂]

    维度建模——数据仓库初步[通俗易懂]本文是《维度建模》后续文章的基础。我们首先从宏观层面上考察数据仓库和商业智能(DataWarehousingandBusinessIntelligence,DW/BI)系统。DW/BI系统首先应该仔细考虑的问题是业务需求。《维度建模》系列文章将紧紧抓住业务需求这一要点,逐步深入探讨逻辑设计、物理设计以及采用有关技术和工具的决策等问题。本文将详细考察数据仓库及商业智能的主要目标,辨析DW/…

    2022年5月7日
    44

发表回复

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

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