GStreamer —— 2.2、Windows下Qt加载GStreamer库后运行 - “教程2:GStreamer 概念“(附:完整源码)

运行效果

简介

上一个教程演示了如何自动构建管道。现在 我们将通过实例化每个元素来手动构建管道 并将它们全部链接在一起。在此过程中,我们将学习:

• 什么是 GStreamer 元素以及如何创建一个。

• 如何将元素相互连接。

• 如何自定义元素的行为。

• 如何观察总线的错误条件并提取信息 来自 GStreamer 消息。

这些元素是 GStreamer 的基本构造块。他们处理 数据从源元素(数据生产者)流向下游 传递给 sink 元素(数据使用者),通过 filter 元素。

GStreamer相关运行库
cpp 复制代码
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/include/gstreamer-1.0/gst
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/include
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/include/gstreamer-1.0
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/include/glib-2.0
INCLUDEPATH += D:/Software/GStreamer/1.0/mingw_x86_64/lib/glib-2.0/include

LIBS += D:/Software/GStreamer/1.0/mingw_x86_64/lib/gstreamer-1.0.lib
LIBS += D:/Software/GStreamer/1.0/mingw_x86_64/lib/glib-2.0.lib
LIBS += D:/Software/GStreamer/1.0/mingw_x86_64/lib/gobject-2.0.lib

完整源码
cpp 复制代码
#include <QCoreApplication>
#include <QDebug>

#include "gst.h"

int tutorial_main()
{
    GstBus *bus;
    GstMessage *msg;
    GstStateChangeReturn ret;

    /* 初始化GStreamer */
    gst_init (nullptr,nullptr);

    /* 创建元素 */
    GstElement *source = gst_element_factory_make ("videotestsrc", "source");       // 元素输出
    GstElement *sink = gst_element_factory_make ("autovideosink", "sink");          // 元素输入

    /* 创建空管道 */
    GstElement *pipeline = gst_pipeline_new ("test-pipeline");
    if (!pipeline || !source || !sink) {g_printerr ("Not all elements could be created.\n");return -1;}

    /* 构建管道 */
    gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
    if (gst_element_link (source, sink) != TRUE)
    {
        g_printerr ("无法链接元素.\n");
        gst_object_unref (pipeline); return -1;
    }

    /* 修改元素的属性 */
    g_object_set (source, "pattern", 1, NULL);

    /* 开始播放 */
    ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
    if (ret == GST_STATE_CHANGE_FAILURE)
    {
        g_printerr ("无法将管道设置为播放状态.\n");
        gst_object_unref (pipeline); return -1;
    }

     /* 用于获取与特定元素相关联的消息总线(message bus)函数。
     * 消息总线是用于在管道的不同部分之间传递消息的机制,例如错误、警告、EOS(End Of Stream)事件等。 */
    bus = gst_element_get_bus (pipeline);
    /* 等待错误或流结束,获取并解析消息 */
    msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
    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 ("从元素接收到错误 %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
            g_printerr ("调试信息: %s\n", debug_info ? debug_info : "none");
            g_clear_error (&err);
            g_free (debug_info);
            break;
        case GST_MESSAGE_EOS: g_print ("流已结束.\n"); break;
        default:
            /* 不应该到达这里,因为我们只要求错误和EOS */
            g_printerr ("收到意外消息.\n"); break;
        }
        gst_message_unref (msg);
    }

    /* 释放资源 */
    gst_object_unref (bus);
    gst_element_set_state (pipeline, GST_STATE_NULL);
    gst_object_unref (pipeline);
    return 0;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

#if 1
    tutorial_main();
#else
    gst_init(nullptr,nullptr);

    const gchar *nano_str;
    guint major,minor, micro,nano;
    gst_version(&major, &minor, &micro,&nano);

    qDebug() << major << " " << minor << " " << micro << " " << nano;
#endif

    return a.exec();
}

关注

笔者 - jxd

相关推荐
FPGAI1 天前
Qt编程之信号与槽
开发语言·qt
只因在人海中多看了你一眼1 天前
B.50.10.09-RPC核心原理与电商应用
qt·网络协议·rpc
yudiandian20141 天前
【QT 5.12.12 下载 Windows 版本】
开发语言·qt
炮院李教员1 天前
使用Qt Core模块(无GUI依赖),确保程序作为后台服务/daemon运行,与任何GUI完全无交互。
开发语言·qt
歪歪1001 天前
Qt Creator 打包应用程序时经常会遇到各种问题
开发语言·c++·qt·架构·编辑器
滴滴滴嘟嘟嘟.1 天前
Qt自定义列表项与QListWidget学习
开发语言·qt·学习
滴滴滴嘟嘟嘟.1 天前
Qt对话框与文件操作学习
开发语言·qt·学习
滴滴滴嘟嘟嘟.1 天前
Qt图表功能学习
开发语言·qt·学习
钱彬 (Qian Bin)1 天前
一文掌握工业缺陷检测项目实战(Pytorch算法训练、部署、C++ DLL制作、Qt集成)
c++·pytorch·python·qt·实战·工业缺陷检测·faster rcnn
FPGAI2 天前
Qt的入门
笔记·qt·学习