Qt优化onvif设备搜索/一键批量搜索/onvif设备模拟器/几千路并发/实时推流/虚拟监控摄像头

一、前言说明

之前做的onvif设备模拟器,吹牛逼说支持千路并发,最近就遇到了真实的客户,真的要500路并发,当然网卡是前提必须满足的,带宽管够,亲自测试,超过150路打开就直接崩溃了,那为什么当时笃定可以上千路呢,因为本地测试的时候,用的64路,一看CPU占用才3%不到,按照这个估计大约的数量,推算得出来的,谁知道上了100路,性能开始指数级下降。还有个现象就是150路的时候,只在程序打开的时候崩溃,而如果是添加过程中,是不会出现的,于是先从这个着手,这个现象其实从推流大全程序的时候就有,当时推流大全就按照定时器排队处理推流,比如间隔0.1s处理一个,这样不会瞬间卡主,所以按照这个思路,模拟器这边也按照定时器排队处理,这样试下来,效果非常好,上了300路一点问题没有,起码程序这边不会崩溃,只是CPU占用非常高,突然想起来之前ABL的作者就亲自测试过各种流媒体服务程序的性能PK,发现mediamtx在windows上垫底,想到这里果断换zlm测试,明显优化了很多,300路占用也极大的降低了资源占用。很是奇怪,mediamtx用的go,而go号称天生高并发,为什么并发这么垃圾,搞不懂,据说在linux上性能还可以。经过这几个折腾后,用户电脑上测试500路也可以了,我这电脑配置带不动500路,几乎占满了。

二、效果图


三、相关地址

  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_simulate

四、功能特点

  1. 标准onvif协议,支持设备搜索、获取参数、快照抓图等。
  2. 支持264/265/aac等标准视音频协议传输。
  3. 支持多路批量onvif设备模拟,每一路都独立的端口。
  4. 支持本地摄像头采集转成onvif,可选择不同的设备、分辨率、帧率等参数。
  5. 支持本地桌面采集转成onvif,可选择不同的屏幕、分辨率、帧率等参数。
  6. 支持各种视频文件和视频流转成onvif,可重新设置编码转换以及分辨率转换。
  7. 支持4K、8K等高清分辨率,不限制分辨率,非264/265会自动转码推流。
  8. 每一路都可以设置统一或者独立的用户验证信息,为空则表示不验证。
  9. 可以把任意内容接入到NVR以及视频监控系统,方便保存录像文件,以便回放可查。
  10. 也可作为压力测试工具,比如模拟几千路onvif设备,让集成平台软件做接入压力测试。
  11. 推出去的流不仅有rtsp格式,还支持rtmp、http、flv、ws-flv、webrtc等方式访问,可以直接网页查看。
  12. 在管理工具上可以看到每一路的推流状况以及分辨率信息,非常直观。
  13. 支持自动重连拉流,重连推流,保证7乘以24小时稳定运行。
  14. 可设置开机自启动运行和后台运行,不显示在任务栏,作为后台服务运行。
  15. 可批量添加文件、添加目录,自动将目录下的所有文件添加到模拟器。
  16. 多功能添加地址面板,可以选择本地设备和监控设备,本地设备会自动识别摄像头设备和桌面设备,监控设备可以选择不同厂家,自动填充对应rtsp格式,填入用户信息即可,可以批量递增添加监控设备。
  17. 可无缝上传到市面上所有的onvif协议设备,包括海康、大华、宇视、华为、天地伟业等,也支持ONVIF Device Manager国际onvif工具。
  18. 支持gb28181设备模拟,具备设备注册、设备注销、设备心跳、设备信息、设备配置、设备状态应答等。
  19. 支持模拟报警和位置上报等,方便平台侧显示对应设备的实时位置。
  20. 支持一键添加批量模拟28181设备,实时显示已注册和已注销状态。
  21. 支持将本地桌面、本地摄像头、任意视频文件、视频流文件、手机摄像头等转换成28181设备,添加到NVR或者国标软件平台。
  22. sip协议同时支持udp和tcp两种通信方式,视频点播同时支持udp/tcp主动/tcp被动三种方式,涵盖所有可能的场景需求。
  23. 无论是onvif设备模拟组件还是28181设备模拟组件,全部原创底层协议解析,纯Qt实现,跨任意平台。
  24. 代码结构框架非常清晰,注释详细,代码精简不繁琐,非常易于学习和移植,可以很容易拓展其他接口需求。
  25. 支持Qt4/Qt5/Qt6以及后续所有版本、所有编译器、所有开发环境。
  26. 支持windows、linux、mac、国产OS、嵌入式linux、RK3588、树莓派、香橙派等系统。

五、相关代码

cpp 复制代码
#include "onvifdeviceserver.h"
#include "onvifdevicesearch.h"
#include "onvifdevicepush.h"
#include "onvifdevicelisten.h"
#include "onvifdevicehelper.h"

OnvifDeviceServer::OnvifDeviceServer(QObject *parent) : QObject(parent)
{
    serverHost = "127.0.0.1";
    serverIp = "127.0.0.1";
    userName = "admin";
    userPwd = "123456";

    //实例化onvif搜索类并关联信号槽
    isStart = false;
    onvifSearch = new OnvifDeviceSearch(this);
    connect(onvifSearch, SIGNAL(sendData(QByteArray)), this, SIGNAL(sendData(QByteArray)));
    connect(onvifSearch, SIGNAL(receiveData(QByteArray)), this, SIGNAL(receiveData(QByteArray)));
    connect(onvifSearch, SIGNAL(receiveInfo(QString)), this, SIGNAL(receiveInfo(QString)));
    connect(onvifSearch, SIGNAL(receiveError(QString)), this, SIGNAL(receiveError(QString)));

    //定时器排队处理
    timerPush = new QTimer(this);
    connect(timerPush, SIGNAL(timeout()), this, SLOT(pushOne()));
}

OnvifDeviceServer::~OnvifDeviceServer()
{
    this->stop();
}

void OnvifDeviceServer::setPara(const QString &serverHost, const QString &serverIp, const QString &userName, const QString &userPwd, int pushInterval)
{
    this->serverHost = serverHost;
    this->serverIp = serverIp;
    this->userName = userName;
    this->userPwd = userPwd;
    this->pushInterval = pushInterval;
}

bool OnvifDeviceServer::start()
{
    foreach (OnvifDeviceListen *listen, listListen) {
        if (!listen->isOk()) {
            listen->start();
        }
    }

    isStart = true;
    this->startPush();
    return onvifSearch->start(serverIp);
}

void OnvifDeviceServer::stop()
{
    timerPush->stop();
    foreach (OnvifDevicePush *push, listPush) {
        push->stop();
    }

    foreach (OnvifDeviceListen *listen, listListen) {
        listen->stop();
    }

    isStart = false;
    onvifSearch->stop();
}

bool OnvifDeviceServer::append(const QString &flag, int port, const QString &mediaUrl, const QString &rtspUrl)
{
    //构建onvif地址/已经存在说明冲突了
    QString hard = OnvifDeviceHelper::getUuid();
    QString addr = QString("http://%1:%2/onvif").arg(serverHost).arg(port);
    if (listFlag.contains(flag)) {
        QMessageBox::critical(NULL, "错误", "推流码重复, 请重新填写!");
        return false;
    } else if (listAddr.contains(addr)) {
        QMessageBox::critical(NULL, "错误", "端口号重复, 请重新填写!");
        return false;
    }

    //启动推流服务
    OnvifDevicePush *push = new OnvifDevicePush;
    connect(push, SIGNAL(pushStart(QString, int, int, bool)), this, SLOT(slot_pushStart(QString, int, int, bool)));
    connect(push, SIGNAL(pushChanged(QString, int)), this, SIGNAL(pushChanged(QString, int)));
    connect(push, SIGNAL(pushImage(QString, QImage)), this, SIGNAL(pushImage(QString, QImage)));
    push->setPara(flag, mediaUrl, rtspUrl);

    //启动监听服务
    OnvifDeviceListen *listen = new OnvifDeviceListen;
    connect(listen, SIGNAL(sendData(QByteArray)), this, SIGNAL(sendData(QByteArray)));
    connect(listen, SIGNAL(receiveData(QByteArray)), this, SIGNAL(receiveData(QByteArray)));
    connect(listen, SIGNAL(receiveInfo(QString)), this, SIGNAL(receiveInfo(QString)));
    connect(listen, SIGNAL(receiveError(QString)), this, SIGNAL(receiveError(QString)));
    connect(listen, SIGNAL(snapshot(QString, QTcpSocket *)), this, SLOT(slot_snapshot(QString, QTcpSocket *)));
    listen->setPara(flag, hard, serverIp, port, addr, rtspUrl);
    listen->setUserInfo(userName, userPwd);

    //处于启动中则立即启动
    if (isStart) {
        //push->start();
        listen->start();
    }

    listFlag << flag;
    listHard << hard;
    listAddr << addr;
    listPush << push;
    listListen << listen;
    this->startPush();
    onvifSearch->setPara(listHard, listAddr);
    return true;
}

void OnvifDeviceServer::remove(const QString &flag)
{
    timerPush->stop();
    int index = listFlag.indexOf(flag);
    if (index >= 0) {
        listPush.at(index)->stop();
        listPush.at(index)->deleteLater();
        listListen.at(index)->stop();
        listListen.at(index)->deleteLater();

        listFlag.removeAt(index);
        listAddr.removeAt(index);
        listPush.removeAt(index);
        listListen.removeAt(index);
        onvifSearch->setPara(listHard, listAddr);
    }
}

void OnvifDeviceServer::clear()
{
    timerPush->stop();
    foreach (OnvifDevicePush *push, listPush) {
        push->stop();
        push->deleteLater();
    }

    foreach (OnvifDeviceListen *listen, listListen) {
        listen->stop();
        listen->deleteLater();
    }

    listFlag.clear();
    listAddr.clear();
    listPush.clear();
    listListen.clear();
    onvifSearch->setPara(listHard, listAddr);
}

void OnvifDeviceServer::appendData(const QString &flag, const QByteArray &data)
{
    int index = listFlag.indexOf(flag);
    if (index >= 0) {
        listPush.at(index)->appendData(data);
    }
}

void OnvifDeviceServer::pushOne()
{
    if (!isStart || pushIndex >= listPush.count() - 1) {
        timerPush->stop();
        emit pushFinsh();
        return;
    }

    pushIndex++;
    if (!listPush.at(pushIndex)->isOk()) {
        listPush.at(pushIndex)->start();
    } else {        
        this->pushOne();
    }
}

void OnvifDeviceServer::startPush()
{
    pushIndex = -1;
    timerPush->stop();
    timerPush->start(pushInterval < 50 ? 50 : pushInterval);
}

void OnvifDeviceServer::slot_snapshot(const QString &flag, QTcpSocket *socket)
{
    int index = listFlag.indexOf(flag);
    if (index >= 0) {
        listPush.at(index)->snap(socket);
    }
}

void OnvifDeviceServer::slot_pushStart(const QString &flag, int width, int height, bool start)
{
    emit pushStart(flag, width, height, start);
    int index = listFlag.indexOf(flag);
    if (index >= 0) {
        listListen.at(index)->setVideoInfo(25, width, height);
    }
}
相关推荐
wd_cloud5 小时前
QT/6.7.2/Creator编译Windows64 MySQL驱动
开发语言·qt·mysql
Source.Liu10 小时前
【LibreCAD】LC_UndoSection 类详解
qt·cad
27399202910 小时前
qt 获取IP地址(公网 本机)
qt·tcp/ip·php
ULTRA??12 小时前
QT向量实现GJK碰撞检测算法几何图形二维版本
c++·qt·算法
我要升天!12 小时前
QT -- QSS界面优化
开发语言·c++·qt
lenkco14 小时前
修改QtConcurrent::run支持任意参数
开发语言·c++·qt
葱卤山猪15 小时前
【Qt】 TCP套接字通信学习文档
qt·学习·tcp/ip
ULTRA??15 小时前
QT向量类实现GJK碰撞检测算法3d版本
c++·qt·算法