【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

相关推荐
Monly214 分钟前
Java(若依):修改Tomcat的版本
java·开发语言·tomcat
boligongzhu5 分钟前
DALSA工业相机SDK二次开发(图像采集及保存)C#版
开发语言·c#·dalsa
Eric.Lee20215 分钟前
moviepy将图片序列制作成视频并加载字幕 - python 实现
开发语言·python·音视频·moviepy·字幕视频合成·图像制作为视频
7yewh7 分钟前
嵌入式Linux QT+OpenCV基于人脸识别的考勤系统 项目
linux·开发语言·arm开发·驱动开发·qt·opencv·嵌入式linux
却道天凉_好个秋10 分钟前
音视频学习(二十四):hls协议
音视频·hls
waicsdn_haha19 分钟前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
_WndProc21 分钟前
C++ 日志输出
开发语言·c++·算法
qq_4335545430 分钟前
C++ 面向对象编程:+号运算符重载,左移运算符重载
开发语言·c++
数据小爬虫@1 小时前
如何高效利用Python爬虫按关键字搜索苏宁商品
开发语言·爬虫·python
ZJ_.1 小时前
WPSJS:让 WPS 办公与 JavaScript 完美联动
开发语言·前端·javascript·vscode·ecmascript·wps