用纯Qt实现GB28181协议/实时视频/云台控制/预置位/录像回放和下载/事件订阅/语音对讲

一、前言

在技术的长河中探索,有些目标一旦确立,便如同璀璨星辰,指引着我们不断前行。早在2014年,我心中就种下了用纯Qt实现GB28181协议的种子,如今回首,一晃十年已逝,好在整体框架和逻辑终于打通,个中滋味,只有自己知晓。

最初接触GB28181协议时,我就发现它远比熟悉的onvif协议复杂。onvif协议在局域网内表现出色,配置简单、使用方便,很多安防设备在局域网环境下借助onvif协议能轻松实现设备间的互联互通。然而,当涉及外网访问时,onvif就显得力不从心,几乎找不到有效的解决办法。在如今监控设备遍布大街小巷,各部门机构都急需外网远程取流的大环境下,GB28181协议应运而生。它作为一套视频监控规范,旨在解决外网访问监控视频的诸多痛点。但GB28181协议也面临着网络通信的固有难题,比如服务端在未收到客户端消息时,无法知晓客户端的具体通信地址,这就导致双方无法直接通信,必须由客户端主动向服务器发送消息来建立联系。

在实现GB28181协议的过程中,选择何种方式解析SIP协议是关键。市面上关于SIP协议的第三方库五花八门,功能看似完备。但经过深思熟虑,我最终决定采用Qt底层的udp通信协议来进行解析。这么做主要有两方面的考量。一方面,从底层入手能让我更深入地理解协议的每一个细节。在解析过程中,我可以根据实际需求打造友好的使用接口,避免了使用第三方库时可能遇到的各种问题,比如繁琐的编译过程,不同版本库之间的兼容性难题等。另一方面,考虑到项目后期的拓展性,从底层"手撸"代码是最好的选择。只有牢牢掌握底层实现,才能灵活应对各种新需求和新场景,将兼容性和易用性放在首位。毕竟,只有真正解决了客户在使用过程中的痛点,产品才有市场价值。

二、效果图




三、相关地址

  1. 国内站点:https://gitee.com/feiyangqingyun
  2. 国际站点:https://github.com/feiyangqingyun
  3. 个人作品:https://blog.csdn.net/feiyangqingyun/article/details/97565652
  4. 文件地址:https://pan.baidu.com/s/1d7TH_GEYl5nOecuNlWJJ7g 提取码:01jf 文件名:bin_video_gb28181。

四、功能特点

  1. 支持设备注册、注销、心跳、校时、注册认证、注销认证等。
  2. 设备上线后可以手动获取设备状态、设备信息、配置信息、预置位信息等。
  3. 设备上线后自动获取设备通道信息,包括中文通道名称。识别到通道上线离线变化,会重新获取该设备的所有通道信息。
  4. 支持视频点播,可以分别点播主码流和子码流,内置rtp解包线程,解包后发给视频播放组件解码播放。
  5. 每个设备每个通道支持点播多个视频,通过ssrc区分,支持共用端口和不同端口收流。
  6. 支持对某个设备下面所有通道、某个通道、某个通道对应的某个流分别关闭。
  7. 支持录像文件查询和回放,回放控制支持暂停播放、继续播放、倍速播放、切换播放进度。
  8. 支持录像文件下载,支持倍速比如8倍速下载,可同时多线程批量下载。
  9. 回放和下载同时支持IPC和NVR,比如摄像头自带的SD存储卡录像文件回放,NVR上的硬盘录像文件回放。
  10. 支持云台控制,向上、向下、向左、向右、左上、右上、左下、右下方位移动,镜头放大缩小,光圈放大缩小,镜头聚焦放焦。
  11. 支持预置位信息的查询、调用、添加、修改、删除等操作。
  12. 自动目录订阅功能,通道上线下线都有对应的信号通知。
  13. 支持警情订阅,各种警情事件比如运动目标检测报警、入侵检测报警、徘徊检测报警等自动上报。
  14. 国标服务同时支持udp和tcp方式,可选只监听一种或者两种都监听,tcp方式自动处理粘包问题。
  15. 收流端口自动纠错,自动跳过被占用的端口,不会出现端口占用导致收流失败的情况。
  16. 支持几千路国标消息交互并发,实时视频流支持64路同时显示,可以拓展更多路数。
  17. 支持阿里云等云服务器,可以分别设置内网监听地址和外网访问地址,一般云服务器上是监听地址用内网,对外访问用外网地址。
  18. 支持视频分发,也就是推流,视频通道打开后可以自动推流到流媒体服务器,其他需要的地方拉流即可,支持rtsp、rtmp、hls、webrtc等方式拉流。
  19. SIP解析和交互采用纯Qt底层代码实现,udp/tcp通信交互,祖传原创代码解析,不依赖任何第三方。
  20. 代码量少,gb28181交互部分共几千行代码,注释详细,接口友好,使用极其简单,提供非常详细的使用示例。
  21. 支持海康、大华、宇视、华为、天地伟业等所有国标设备。
  22. 支持所有Qt版本和编译器以及操作系统,包括但不限于win、linux、mac、android、嵌入式linux、树莓派香橙派、国产os等。

五、相关代码

cpp 复制代码
#include "frmconfig.h"
#include "frmserver.h"
#include "ui_frmserver.h"
#include "qthelper.h"
#include "apphelper.h"
#include "rtpthread.h"
#include "gb28181server.h"
#include "gb28181helper.h"

frmServer::frmServer(QWidget *parent) : QWidget(parent), ui(new Ui::frmServer)
{
    ui->setupUi(this);
    this->initForm();
    this->initConfig();
}

frmServer::~frmServer()
{
    delete ui;
}

void frmServer::closeEvent(QCloseEvent *)
{
    if (AppConfig::ServerStart) {
        on_btnStart_clicked();
    }

    qApp->quit();
}

void frmServer::initForm()
{
    ui->tabPlayback->setEnabled(false);
    ui->tabDownload->setEnabled(false);
    ui->widgetControl->setEnabled(false);
    ui->widget->setFixedWidth(AppData::RightWidth);
    connect(ui->tabPreview, SIGNAL(selectVideo(QString, QString)), this, SLOT(selectVideo(QString, QString)));

    ui->treeWidget->setAnimated(false);
    ui->treeWidget->setIndentation(15);
    ui->treeWidget->setExpandsOnDoubleClick(false);

    //立即启动服务
    server = NULL;
    if (AppConfig::ServerStart) {
        on_btnStart_clicked();
    }
}

void frmServer::initConfig()
{
    ui->tabWidget->setCurrentIndex(AppConfig::TabIndexMain);
    connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(saveConfig()));

    ui->cboxFlag->addItem(AppConfig::FilterType == 0 ? "00000000000000000000" : "0.0.0.0");
    ui->cboxFlag->lineEdit()->setText(AppConfig::FilterFlag);
    connect(ui->cboxFlag->lineEdit(), SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
}

void frmServer::saveConfig()
{
    AppConfig::TabIndexMain = ui->tabWidget->currentIndex();
    AppConfig::FilterFlag = ui->cboxFlag->lineEdit()->text();
    AppConfig::writeConfig();
}

void frmServer::receiveEvent(GB28181Event event)
{
    if (event.alarmMethod != 5) {
        return;
    }

    //设备中的某个通道离线和上线
    QTreeWidgetItem *itemChannel = AppHelper::findItem(ui->treeWidget, event.deviceId, event.channelId);
    if (itemChannel) {
        if (event.alarmInfo == 13) {
            itemChannel->setDisabled(true);
            ui->tabPreview->closeVideo(event.deviceId, event.channelId);
        } else if (event.alarmInfo == 14) {
            itemChannel->setDisabled(false);
        }
    }
}

void frmServer::deviceChanged(const QString &deviceId, bool online)
{
    //关闭对应的画面
    if (!online) {
        ui->tabPreview->closeVideo(deviceId);
    }

    QList<GB28181Device> devices = server->getDevices();
    GB28181Device device = GB28181Helper::getDevice(deviceId, devices);
    QString text = deviceId + " [" + device.deviceName + "]";

    QTreeWidgetItem *itemDevice = AppHelper::findItem(ui->treeWidget, deviceId, "");
    if (itemDevice) {
        itemDevice->setText(0, text);
        itemDevice->setDisabled(!online);
        return;
    }

    //不存在则添加顶层节点
    QTreeWidgetItem *item = new QTreeWidgetItem;
    item->setText(0, text);
    item->setData(0, Qt::UserRole, deviceId);
    ui->treeWidget->insertTopLevelItem(0, item);

    //添加到下拉框
    QString flag = (AppConfig::FilterType == 0 ? device.deviceId : device.deviceIp);
    if (ui->cboxFlag->findText(flag) < 0) {
        ui->cboxFlag->addItem(flag);
    }
}

void frmServer::channelChanged(const QString &deviceId)
{
    //每次都清空通道再重新添加
    QList<GB28181Device> devices = server->getDevices();
    QTreeWidgetItem *itemDevice = AppHelper::findItem(ui->treeWidget, deviceId, "");
    if (itemDevice) {
        qDeleteAll(itemDevice->takeChildren());
        QStringList ids, names;
        GB28181Helper::getChannelInfo(deviceId, devices, ids, names);
        for (int i = 0; i < ids.count(); ++i) {
            QTreeWidgetItem *itemChannel = new QTreeWidgetItem(itemDevice);
            itemChannel->setText(0, ids.at(i) + " [" + names.at(i) + "]");
            itemChannel->setData(0, Qt::UserRole, ids.at(i));
        }
    }

    //通过单次定时器去执行/防止频繁上线期间卡主界面
    static QTimer *timer = NULL;
    if (!timer) {
        timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(finshed()));
        timer->setSingleShot(true);
        timer->setInterval(300);
    }

    timer->stop();
    timer->start();
}

void frmServer::finshed()
{
    //展开所有节点
    ui->treeWidget->expandAll();
    //自动调整列宽
    ui->treeWidget->resizeColumnToContents(0);
}

void frmServer::on_btnStart_clicked()
{
    if (ui->btnStart->text() == "启动服务") {
        ui->tabDebug->clear();
        ui->tabEvent->clear();
        if (AppConfig::ServerIp == "0.0.0.0") {
            QtHelper::showMessageBoxError("请先打开系统设置, 选择网卡地址!");
            return;
        }

        server = new GB28181Server;
        connect(server, SIGNAL(sendData(QString, int, QString, QString)), ui->tabDebug, SLOT(sendData(QString, int, QString, QString)));
        connect(server, SIGNAL(receiveData(QString, int, QString, QString)), ui->tabDebug, SLOT(receiveData(QString, int, QString, QString)));
        connect(server, SIGNAL(receiveInfo(QString, int, QString, QString)), ui->tabDebug, SLOT(receiveInfo(QString, int, QString, QString)));
        connect(server, SIGNAL(playStart(QString, int, QString, int, int)), ui->tabDebug, SLOT(playStart(QString, int, QString, int, int)));

        connect(server, SIGNAL(receiveEvent(GB28181Event)), this, SLOT(receiveEvent(GB28181Event)));
        connect(server, SIGNAL(receiveEvent(GB28181Event)), ui->tabEvent, SLOT(receiveEvent(GB28181Event)));
        connect(server, SIGNAL(receiveRecord(QList<GB28181Record>)), ui->tabPlayback, SLOT(receiveRecord(QList<GB28181Record>)));
        connect(server, SIGNAL(receiveRecord(QList<GB28181Record>)), ui->tabDownload, SLOT(receiveRecord(QList<GB28181Record>)));
        connect(server, SIGNAL(receiveStatus(GB28181Status)), ui->tabPlayback, SLOT(receiveStatus(GB28181Status)));
        connect(server, SIGNAL(receiveStatus(GB28181Status)), ui->tabDownload, SLOT(receiveStatus(GB28181Status)));

        connect(server, SIGNAL(deviceChanged(QString, bool)), this, SLOT(deviceChanged(QString, bool)));
        connect(server, SIGNAL(channelChanged(QString)), this, SLOT(channelChanged(QString)));
        connect(server, SIGNAL(presetChanged(QStringList, QStringList)), ui->widgetControl, SLOT(presetChanged(QStringList, QStringList)));

        GB28181ServerPara para;
        para.serverId = AppConfig::ServerId;
        para.serverRealm = AppConfig::ServerRealm;
        para.serverHost = AppConfig::ServerHost;
        para.serverIp = AppConfig::ServerIp;
        para.serverPort = AppConfig::ServerPort;
        para.serverPwd = AppConfig::ServerPwd;
        para.timeout = AppConfig::Timeout;
        server->setServerPara(para);
        server->start((ListenMode)AppConfig::ListenMode);

        ui->btnStart->setText("停止服务");
        ui->tabPreview->setServer(server);
        ui->tabPlayback->setServer(server);
        ui->tabDownload->setServer(server);
        ui->widgetControl->setServer(server);
        ui->tabPlayback->setEnabled(true);
        ui->tabDownload->setEnabled(true);
        ui->widgetControl->setEnabled(true);
    } else {
        ui->treeWidget->clear();
        ui->treeWidget->resizeColumnToContents(0);
        ui->btnStart->setText("启动服务");
        ui->tabPreview->closeAll();
        ui->tabPlayback->closeAll();
        ui->tabDownload->closeAll();
        ui->widgetControl->closeAll();
        ui->tabPlayback->setEnabled(false);
        ui->tabDownload->setEnabled(false);
        ui->widgetControl->setEnabled(false);

        //停顿下处理事件
        qApp->processEvents();
        server->stop();
        server->deleteLater();
        server = NULL;
        RtpThread::Port = 6900;
    }

    //按钮触发的要保存启动状态
    if (sender() == ui->btnStart) {
        AppConfig::ServerStart = (ui->btnStart->text() == "停止服务");
        AppConfig::writeConfig();
    }
}

void frmServer::on_btnConfig_clicked()
{
    static frmConfig *config = new frmConfig;
    config->show();
    config->activateWindow();
}

void frmServer::selectVideo(const QString &deviceId, const QString &channelId)
{
    //视频通道按下自动选中设备通道节点
    QTreeWidgetItem *itemChannel = AppHelper::selectItem(ui->treeWidget, deviceId, channelId);
    if (itemChannel) {
        on_treeWidget_itemClicked(itemChannel, 0);
    }
}

void frmServer::itemClicked(QTreeWidgetItem *item, bool dbClick)
{
    QString deviceId, channelId;
    AppHelper::getItemId(item, deviceId, channelId);
    ui->tabPlayback->setId(deviceId, channelId);
    ui->tabDownload->setId(deviceId, channelId);
    ui->widgetControl->setId(deviceId, channelId);
    if (dbClick) {
        ui->tabPreview->openVideo(deviceId, channelId);
    }
}

void frmServer::on_treeWidget_itemClicked(QTreeWidgetItem *item, int)
{
    this->itemClicked(item, false);
}

void frmServer::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int)
{
    this->itemClicked(item, true);
}
相关推荐
mahuifa2 小时前
pyqt环境配置
python·qt·pycharm·pyqt·环境配置
kchmmd2 小时前
基于QtC++音乐播放器whisper语音转文字歌词解析
c++·qt
EasyDSS2 小时前
城市应急安防系统EasyCVR视频融合平台:如何实现多源视频资源高效汇聚与应急指挥协同
大数据·网络·网络协议·音视频
hello_simon2 小时前
小白工具视频转gif,支持在线gif或视频互转,批量转换,免费在线使用,无需下载
开发语言·php·音视频
island13143 小时前
【QT】QT界面的美容院 -- QSS
开发语言·qt
溟洵4 小时前
【C++ Qt】认识Qt、Qt 项目搭建流程(图文并茂、通俗易懂)
开发语言·c++·qt
Zfox_5 小时前
【QT】 常用控件【输入类】
c++·qt·qt5·客户端开发
FreeLikeTheWind.14 小时前
Qt问题之 告别软件因系统默认中文输入法导致错误退出的烦恼
开发语言·c++·windows·经验分享·qt
爱看书的小沐15 小时前
【小沐学GIS】基于C++绘制三维数字地球Earth(QT5、OpenGL、GIS、卫星)第五期
c++·qt·opengl·imgui·地球·卫星·gis地球
suki45226292617 小时前
音视频转换器 AV 接口静电保护方案
音视频