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)
上一篇 2022年10月17日 下午2:46
下一篇 2022年10月17日 下午3:00


相关推荐

  • 智谱AI 的API接入教程:最全RESTful接口调用指南(含Python示例代码)

    智谱AI 的API接入教程:最全RESTful接口调用指南(含Python示例代码)

    2026年3月12日
    2
  • 双IP双线路实现方式 先来说说双线单IP和双线双IP的区别

    双IP双线路实现方式 先来说说双线单IP和双线双IP的区别双 IP 双线路实现方式双 IP 双线路实现方式是指在一台服务器上安装两块网卡 分别接入电信网线与网通网线并设置一个网通 IP 与一个电信 IP 这样一台服务器上就有了两个 IP 地址 需要在服务器上添加网通或电信的路由表来实现网通用户与电信用户分别从不同的线路访问 双 IP 双线路具有常用的两种使用方式 1 nbsp ICP 用户在网站设置两个 IP 地址不同的链接 网通用户点击网通 IP 访问服务器 电信用户点击电信 IP 访问 2 nbsp 使用 BIND9 DNS 服务器软件 对不同的 IP 地址请求返回不同的服务器 IP 的功能来实现网通用户请求域名时返回网通

    2026年1月17日
    2
  • GeoDa空间计量(五)——空间计量模型

    GeoDa空间计量(五)——空间计量模型GeoDa 空间计量 四 空间计量模型 OSL 模型空间滞后模型空间误差模型自变量空间滞后模型空间杜宾模型空间杜宾误差模型本文以 1984 年哥伦布市的俄亥俄州的 49 个街区的数据为基础 构建 OLS 模型 空间滞后模型 空间误差模型 自变量空间滞后模型 空间杜宾模型 空间杜宾误差模型 具体数据说明见表 1 只对使用的变量进行说明 因 GeoDa 只能对截面数据进行分析 故本文的所有分析都是基于截面数据 变量说明 AREA 空间单元的面积 PERIMETRE 空间单元的周长 CULUMBUS

    2026年3月19日
    2
  • iPhone各机型屏幕尺寸

    iPhone各机型屏幕尺寸机型 尺寸 点(Point) pixel iPhone4/4s 3.5英寸 320×480 960×640 iPhone5/5s/SE 4.0英寸 320×568 640×1136 iPhone6/6s/7/8 4.7英寸 375×667 750×1334 iPhone6p/6sp/7p/8p 5.5英寸 414×736 1242×2208 iPhoneX/…

    2022年5月14日
    105
  • from flask import jsonify

    from flask import jsonifyjsonify是flask中的扩展包,可以将数据转换成json数据。#打开已新建的文件,导入Flask,jsonifyfromflaskimportFlask,jsonify#调用Flask(__name__),并赋值给变量appapp=Flask(__name__)#定义一个json对象数据,然后赋值给变量datadata=[{“sname”:”朱华”,”age”:”20″,”sex”:”男”},{“sname”:”张素”,”age”:”30″,”sex”

    2022年5月23日
    37
  • 软件测试基础知识 – 说一下手动测试与自动化测试的优缺点

    软件测试基础知识 – 说一下手动测试与自动化测试的优缺点分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.netDefinitionEncapsulatearequestasanobject,therebylettingyouparameterizeclientswithdifferentrequests,queueorl…

    2022年6月26日
    27

发表回复

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

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