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


相关推荐

  • 验证二叉搜索树 leetcode_二叉树的最长路径

    验证二叉搜索树 leetcode_二叉树的最长路径重写equal()时为什么也得重写hashCode()之深度解读equal方法与hashCode方法渊源 原创  2016年05月08日 23:14:19 标签:java equal方法重写 /java /重写equals方法和hashCode方 10077 转载请注明出处: http://blog.csdn.net/javazejian/art…

    2022年8月9日
    7
  • EasyBoot用户名注册码

    EasyBoot用户名注册码用户名:中华人民共和国注册码:2898-5448-5603-BB2D

    2022年7月4日
    21
  • vue 父组件调用子组件_react父组件向子组件传值

    vue 父组件调用子组件_react父组件向子组件传值Vue中子组件调用父组件的三种方法:1.直接在子组件中通过“this.$parent.event”来调用父组件的方法。父组件<template><div><child></child></div></template><script>importchildfrom’./components/childcomponent’;exportdefault{co

    2022年10月3日
    2
  • 嵌入式软件架构设计

    嵌入式软件架构设计摘要在开发中一直觉得好的软件架构可以做到事半功倍 而且可以做到代码复用和移植 但是如果没有架构或者说架构很差 那么移植将是很痛苦的事 特别是对不熟悉改功能的人来讲还不如自己写呢 移植不对那将是很头疼的事 所以随着开发越来越多 渐渐的发现我们考虑问题应该从广度和深度来考虑 做新项目的时候 更应该考虑到以后出现的可能性 比如说需求变更 底层变更 所以这个时候软件的架构和程序模块化就很重要

    2025年8月10日
    2
  • sstream函数

    sstream函数使用stringstream对象简化类型转换C++标准库中的提供了比ANSIC的更高级的一些功能,即单纯性、类型安全和可扩展性。在本文中,我将展示怎样使用这些库来实现安全和自动的类型转换。为什么要学习如果你已习惯了风格的转换,也许你首先会问:为什么要花额外的精力来学习基于的类型转换呢?也许对下面一个简单的例子的回顾能够说服你。假设你想用sprintf()函数将一个

    2022年6月4日
    34
  • Springboot + Spring Security + jwt-token实现权限认证

    Springboot + Spring Security + jwt-token实现权限认证

    2021年8月31日
    55

发表回复

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

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