【QGroundControl二次开发】八. QT实现播放gstreamer视频

上篇写到如何搭建gstreamer在Windows和linux下的环境,以及新建VS工程解码视频流。gstreamer 配置+解析编解码

本篇主要讲述c++源码移植到QT工程。

一. QT工程配置

gstreamer环境参考上面链接。

在项目的.pro文件中加入如下代码。(以linux项目为例,Windows项目需要修改对应路径)

bash 复制代码
INCLUDEPATH += /usr/include/glib-2.0
INCLUDEPATH += /usr/lib/x86_64-linux-gnu/glib-2.0/include
INCLUDEPATH += /usr/include/gstreamer-1.0
INCLUDEPATH += /usr/lib/x86_64-linux-gnu/gstreamer-1.0/include

LIBS += -lgstreamer-1.0 -lgobject-2.0 -lglib-2.0 -lgstvideo-1.0 -lgstapp-1.0 -lgstbase-1.0

二. 项目代码

2.1 项目结构如下图所示

2.2 代码

本项目为测试项目,下面为main.cpp代码,用来解析书树莓派发送的RTP视频流,别的代码为新建工程自动生成原样未动。

cpp 复制代码
#include <QApplication>
#include <QWidget>
#include <QtConcurrent/QtConcurrent>
#include <gst/gst.h>
#include <gst/video/videooverlay.h>
#include "ui_mainwindow.h"

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

    QApplication a(argc, argv);

    GstElement *pipeline, *udpsrc, *capsfilter, *queue, *rtph264depay, *h264parse, *avdec_h264, *videoconvert, *vsink, *fpssink;
    GstCaps *caps;

    GstStateChangeReturn ret;

    QWidget *window = new QWidget();
    window->resize(1920, 1080);
    window->show();
    WId xwinid = window->winId();

    // 初始化 GStreamer
    gst_init(NULL, NULL);

    // 创建元素
    pipeline = gst_pipeline_new("my-pipeline");
    udpsrc = gst_element_factory_make("udpsrc", "udpsrc");
    capsfilter = gst_element_factory_make("capsfilter", "capsfilter");
    queue = gst_element_factory_make("queue", "queue");

    h264parse = gst_element_factory_make("h264parse", "h264parse");
    avdec_h264 = gst_element_factory_make("avdec_h264", "avdec_h264");
    rtph264depay = gst_element_factory_make("rtph264depay", "rtph264depay");
    videoconvert = gst_element_factory_make("videoconvert", "videoconvert");
    fpssink = gst_element_factory_make("fpsdisplaysink", "fpssink");
    vsink = gst_element_factory_make("xvimagesink", "vsink");//glimagesink

    if (!pipeline || !udpsrc || !capsfilter || !queue || !rtph264depay || !h264parse || !avdec_h264 || !videoconvert || !fpssink || !vsink)
    {
        g_printerr("Failed to create elements. Exiting.\n0000000");
        return -1;
    }

    // 设置 udpsrc 元素的参数
    g_object_set(udpsrc, "port", 14556, NULL);

    // 创建 caps    
    /*"depth", G_TYPE_STRING, "8",
       "width", G_TYPE_STRING, "1920",
    	"height", G_TYPE_STRING, "1080",*/
    caps = gst_caps_new_simple("application/x-rtp",
                               "media", G_TYPE_STRING, "video",
                               "clock-rate", G_TYPE_INT, 90000,
                               "encoding-name", G_TYPE_STRING, "H264",
                               NULL);
    g_object_set(capsfilter, "caps", caps, NULL);
    gst_caps_unref(caps);


    // 将元素添加到管道中
    gst_bin_add_many(GST_BIN(pipeline), udpsrc, capsfilter, queue, rtph264depay, h264parse, avdec_h264, videoconvert, vsink, NULL);

    // 连接元素
//    if (!gst_element_link_many(udpsrc, capsfilter, queue, rtph264depay, h264parse, avdec_h264, videoconvert, vsink, NULL)) {
//        g_printerr("Failed to link elements. Exiting.\n11111111111");
//        gst_object_unref(pipeline);
//        return -1;
//    }
    // 连接元素
    if (!gst_element_link(udpsrc, capsfilter)) {
        g_printerr("Failed to link udpsrc and capsfilter. Exiting.\n");
        gst_object_unref(pipeline);
        return -1;
    }
    if (!gst_element_link(capsfilter, queue)) {
        g_printerr("Failed to link capsfilter and queue. Exiting.\n");
        gst_object_unref(pipeline);
        return -1;
    }
    if (!gst_element_link(queue, rtph264depay)) {
        g_printerr("Failed to link queue and rtph264depay. Exiting.\n");
        gst_object_unref(pipeline);
        return -1;
    }
    if (!gst_element_link(rtph264depay, h264parse)) {
        g_printerr("Failed to link rtph264depay, h264parse. Exiting.\n");
        gst_object_unref(pipeline);
        return -1;
    }
    if (!gst_element_link(h264parse, avdec_h264)) {
        g_printerr("Failed to link qh264parse, avdec_h264. Exiting.\n");
        gst_object_unref(pipeline);
        return -1;
    }
    if (!gst_element_link(avdec_h264, videoconvert)) {
        g_printerr("Failed to link avdec_h264, videoconvert. Exiting.\n");
        gst_object_unref(pipeline);
        return -1;
    }
    if (!gst_element_link(videoconvert, vsink)) {
        g_printerr("Failed to link avideoconvert, vsink. Exiting.\n");
        gst_object_unref(pipeline);
        return -1;
    }


    // 链接QT界面
    gst_video_overlay_set_window_handle (GST_VIDEO_OVERLAY (vsink), xwinid);

    // 设置管道状态为播放
    ret = gst_element_set_state(pipeline, GST_STATE_PLAYING);
    if (ret == GST_STATE_CHANGE_FAILURE) {
        g_printerr("Failed to set pipeline state to PLAYING. Exiting.\n");
        gst_object_unref(pipeline);
        return -1;
    }


    auto res = a.exec();

    // 释放资源
    gst_element_set_state(pipeline, GST_STATE_NULL);
    gst_object_unref(pipeline);

    return res;
}

三. 测试

配置无人机视频转发目标IP到本机,端口与上面代码中定义的一致。

运行程序:

四. 源码

下载本文源码


参考:

https://blog.csdn.net/Alon1787/article/details/135908723

https://blog.csdn.net/Alon1787/article/details/135107958

相关推荐
DS小龙哥几秒前
QT For Android开发-打开PPT文件
android·qt·powerpoint
青柠视频云1 分钟前
青柠视频云——视频丢包(卡顿、花屏、绿屏)排查
服务器·网络·音视频
liO_Oil1 分钟前
(2024.9.19)在Python的虚拟环境中安装GDAL
开发语言·python·gdal安装
万物得其道者成16 分钟前
React Zustand状态管理库的使用
开发语言·javascript·ecmascript
学步_技术22 分钟前
Python编码系列—Python抽象工厂模式:构建复杂对象家族的蓝图
开发语言·python·抽象工厂模式
wn5311 小时前
【Go - 类型断言】
服务器·开发语言·后端·golang
Hello-Mr.Wang1 小时前
vue3中开发引导页的方法
开发语言·前端·javascript
救救孩子把1 小时前
Java基础之IO流
java·开发语言
WG_171 小时前
C++多态
开发语言·c++·面试
宇卿.1 小时前
Java键盘输入语句
java·开发语言