• 118阅读
  • 0回复

头发快掉光/纯Qt手撕onvif协议/onvif协议服务端/onvif协议设备端/onvif设备模拟器/onvif虚拟监控设备 [复制链接]

上一主题 下一主题
离线liudianwu
 



## 一、前言说明
按道理onvif设备端的程序都是下位机实现的,也就是直接运行在类似摄像头的设备上的,为什么还考虑用Qt去实现这个设备端协议,一方面因为需要测试模拟,还一个方面对应程序员来说最容易忽略,那就是真实的需求,比如大量的收银系统运行的电脑,很多时候有个监控设备对着收银台,但是具体电脑上的操作详情,看的不是很仔细,这个时候就希望将整个桌面作为一个摄像头通道,添加到NVR中,进行录像保存等,到时候万一出了问题,可以调阅查看对应位置收银员的操作,对应电脑上的操作,到底是哪里出了问题,或者收银员有没有用电脑在干其他事情等,可以很方便的取证。

开发视频监控平台软件时,通常需要外接ONVIF设备进行测试。然而,实际设备可能无法随时获取,比如设备被占用、在家调试时无法连接,或者需要模拟几千路设备进行压力测试。因此,我们需要一个ONVIF设备模拟器,能够将视频文件模拟成ONVIF摄像头,无需依赖真实硬件,并能灵活调整模拟设备数量,甚至用于性能测试。例如,某些监控平台号称支持数千路接入或64路同时显示,通过模拟器可以真实测试其性能表现,并监控CPU/GPU资源占用情况。

整个ONVIF模拟器的实现可分为三个关键部分:组播搜索、ONVIF协议交互和RTSP推流。其中,组播搜索用于设备发现,ONVIF协议交互处理请求与响应,RTSP推流则提供视频数据。如果仅需协议测试而不需要视频流,可以省略RTSP部分。在Qt中实现组播时,需要注意多网卡环境下的稳定性问题。例如,joinMulticastGroup的第二个参数用于指定网卡,若不指定,组播可能随机绑定到不同网卡,导致搜索时断时续。这个问题曾耗费大量时间排查,最终通过抓包分析才找到原因。

目前,许多ONVIF设备端的实现依赖第三方库(如gSOAP),虽然可用,但存在接口复杂、函数命名混乱等问题,部分功能甚至需要自行修复。为了更高的可控性和灵活性,我们决定基于底层协议解析来实现,直接通过TCP监听处理ONVIF请求,动态构造XML响应。Qt的网络通信能力足够强大,配合抓包分析,完全可以自主实现完整的ONVIF设备模拟,避免第三方库的限制,同时提升可扩展性。

## 二、效果图
window.open('http://www.qtcn.org/bbs/attachment/Mon_2508/44_110085_e043649db47948b.jpg?325');" style="max-width:700px;max-height:700px;" onload="if(is_ie6&&this.offsetWidth>700)this.width=700;" >



## 三、相关地址
1. 国内站点:[https://gitee.com/feiyangqingyun](https://gitee.com/feiyangqingyun)
2. 国际站点:[https://github.com/feiyangqingyun](https://github.com/feiyangqingyun)
3. 个人作品:[https://blog.csdn.net/feiyangqingyun/article/details/97565652](https://blog.csdn.net/feiyangqingyun/article/details/97565652)
4. 文件地址:[https://pan.baidu.com/s/1d7TH_GEYl5nOecuNlWJJ7g](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 "onviflistenhelper.h"
#include "onvifdevicehelper.h"

bool OnvifListenHelper::checkData(const QByteArray &buffer, QString &cmd, QString &uuid, const QString &userName, const QString &userPwd)
{
    int index = buffer.indexOf("\r\n\r\n");
    if (index < 0) {
        return false;
    }

    //取出命令关键字
    QByteArray body = buffer.mid(index + 4);
    QString value = OnvifDeviceHelper::getValue3(body, "Body");
    int index1 = value.indexOf("<");
    int index2 = value.indexOf(">");
    cmd = value.mid(index1, index2);
    cmd.replace("<", "");
    cmd.replace(">", "");
    cmd.replace("/", "");
    cmd.replace("\r\n", "");
    cmd = cmd.split(" ").first();
    cmd = cmd.split(":").last();
    //qDebug() << TIMEMS << cmd << value;

    //用户信息为空则返回
    if (userName.isEmpty() || userPwd.isEmpty()) {
        return true;
    }

    static QStringList cmds = QStringList() << "GetSystemDateAndTime";
    if (cmds.contains(cmd)) {
        return true;
    }

    //部分指令要取出uuid
    if (cmd == "CreatePullPointSubscription" || cmd == "Renew") {
        uuid = OnvifDeviceHelper::getMessageID(body);
        //qDebug() << TIMEMS << cmd << uuid;
    }

    //取出用户信息
    QString Username = OnvifDeviceHelper::getValue5(body, "Username");
    QString Password = OnvifDeviceHelper::getValue5(body, "Password");
    QString Nonce = OnvifDeviceHelper::getValue5(body, "Nonce");
    QString Created = OnvifDeviceHelper::getValue5(body, "Created");

    //验证用户信息
    QByteArray Nonce2 = QByteArray::fromBase64(Nonce.toLatin1());
    QByteArray Password2 = Nonce2 + Created.toLatin1() + userPwd.toLatin1();
    Password2 = QCryptographicHash::hash(Password2, QCryptographicHash::Sha1).toBase64();
    //qDebug() << TIMEMS << cmd << Username << Nonce << Created << Password << Password2;
    if (userName != Username || Password != Password2) {
        return false;
    }

    return true;
}

void OnvifListenHelper::sendImage(const QImage &image, QTcpSocket *socket)
{
    QBuffer buffer;
    buffer.open(QIODevice::ReadWrite);
    image.save(&buffer, "jpg");
    QByteArray body = buffer.buffer();

    QByteArray data;
    data.append("HTTP/1.1 200 OK\r\n");
    data.append("Connection: close\r\n");
    data.append("Content-Length: ");
    data.append(QString::number(body.size()).toLatin1() + "\r\n");
    data.append("Content-Type: image/jpeg\r\n");
    data.append("\r\n");
    data.append(body);
    socket->write(data);
}

QByteArray OnvifListenHelper::getData(const QString &body, const QString &flag)
{
    QStringList list;
    list << QString("HTTP/1.1 %1").arg(flag);
    list << QString("Connection: close");
    list << QString("Content-Length: %1").arg(body.size());
    list << QString("Content-Type: application/soap+xml; charset=utf-8");
    //list << QString("CSeq: 0");
    list << "" << body;
    QString data = list.join("\r\n");
    return data.toUtf8();
}

QByteArray OnvifListenHelper::getData(const QString &cmd, const QString &uuid, const QString &onvifUrl, const QString &rtspUrl, int videoFps, int videoWidth, int videoHeight)
{
    QByteArray data;
    if (cmd == "GetSystemDateAndTime") {
        data = OnvifListenHelper::getSystemDateAndTime();
    } else if (cmd == "GetServices") {
        data = OnvifListenHelper::getServices(onvifUrl);
    } else if (cmd == "GetCapabilities") {
        data = OnvifListenHelper::getCapabilities(onvifUrl);
    } else if (cmd == "GetProfile") {
        data = OnvifListenHelper::getProfile(videoWidth, videoHeight);
    } else if (cmd == "GetProfiles") {
        data = OnvifListenHelper::getProfiles(videoWidth, videoHeight);
    } else if (cmd == "GetStreamUri") {
        data = OnvifListenHelper::getStreamUri(rtspUrl);
    } else if (cmd == "GetVideoSources") {
        data = OnvifListenHelper::getVideoSources(videoFps, videoWidth, videoHeight);
    } else if (cmd == "GetVideoSourceConfiguration") {
        data = OnvifListenHelper::getVideoSourceConfiguration(videoWidth, videoHeight);
    } else if (cmd == "GetOptions") {
        data = OnvifListenHelper::getOptions();
    } else if (cmd == "GetImagingSettings") {
        data = OnvifListenHelper::getImagingSettings();
    } else if (cmd == "GetMoveOptions") {
        data = OnvifListenHelper::getMoveOptions();
    } else if (cmd == "GetDeviceInformation") {
        data = OnvifListenHelper::getDeviceInformation();
    } else if (cmd == "GetScopes") {
        data = OnvifListenHelper::getScopes();
    } else if (cmd == "GetHostname") {
        data = OnvifListenHelper::getHostname();
    } else if (cmd == "GetNetworkDefaultGateway") {
        //网关强制把末尾地址改成1
        QString ip = OnvifDeviceHelper::getUrlIP(onvifUrl);
        ip = ip.section('.', 0, 2) + ".1";
        data = OnvifListenHelper::getNetworkDefaultGateway(ip);
    } else if (cmd == "GetDNS") {
        data = OnvifListenHelper::getDNS();
    } else if (cmd == "GetNetworkInterfaces") {
        QString ip = OnvifDeviceHelper::getUrlIP(onvifUrl);
        data = OnvifListenHelper::getNetworkInterfaces(ip);
    } else if (cmd == "GetAnalyticsModules") {
        data = OnvifListenHelper::getAnalyticsModules();
    } else if (cmd == "GetSupportedAnalyticsModules") {
        data = OnvifListenHelper::getSupportedAnalyticsModules();
    } else if (cmd == "GetVideoEncoderConfigurationOptions") {
        data = OnvifListenHelper::getVideoEncoderConfigurationOptions();
    } else if (cmd == "CreatePullPointSubscription") {
        QString url = onvifUrl;//"http://192.168.0.110:8089";
        //data = OnvifListenHelper::createPullPointSubscription(url, uuid);
    } else if (cmd == "Renew") {
        //data = OnvifListenHelper::renew(uuid);
    } else if (cmd == "PullMessages") {
        //data = OnvifListenHelper::pullMessages();
    } else if (cmd == "GetSnapshotUri") {
        data = OnvifListenHelper::getSnapshotUri(onvifUrl);
    }

    //未知指令应答
    if (data.isEmpty()) {
        data = OnvifListenHelper::getDefault(cmd);
    }

    return data;
}

QByteArray OnvifListenHelper::getSystemDateAndTime()
{
    QDateTime now = QDateTime::currentDateTimeUtc();
    QDate date = now.date();
    QTime time = now.time();
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetSystemDateAndTime.xml");
    body = body.arg(time.hour()).arg(time.minute()).arg(time.second()).arg(date.year()).arg(date.month()).arg(date.day());
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getServices(const QString &url)
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetServices.xml");
    body = body.arg(url);
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getCapabilities(const QString &url)
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetCapabilities.xml");
    body = body.arg(url);
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getProfile(int width, int height)
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetProfile.xml");
    body = body.arg(width).arg(height);
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getProfiles(int width, int height)
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetProfiles.xml");
    body = body.arg(width).arg(height);
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getStreamUri(const QString &url)
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetStreamUri.xml");
    body = body.arg(url);
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getVideoSources(int fps, int width, int height)
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetVideoSources.xml");
    body = body.arg(fps).arg(width).arg(height);
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getVideoSourceConfiguration(int width, int height)
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetVideoSourceConfiguration.xml");
    body = body.arg(width).arg(height);
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getOptions()
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetOptions.xml");
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getImagingSettings()
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetImagingSettings.xml");
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getMoveOptions()
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetMoveOptions.xml");
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getDeviceInformation()
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetDeviceInformation.xml");
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getScopes()
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetScopes.xml");
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getHostname()
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetHostname.xml");
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getNetworkDefaultGateway(const QString &ip)
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetNetworkDefaultGateway.xml");
    body = body.arg(ip);
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getDNS()
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetDNS.xml");
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getNetworkInterfaces(const QString &ip)
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetNetworkInterfaces.xml");
    body = body.arg("15-A3-65-32-B5-E8").arg(ip);
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getAnalyticsModules()
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetAnalyticsModules.xml");
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getSupportedAnalyticsModules()
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetSupportedAnalyticsModules.xml");
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getVideoEncoderConfigurationOptions()
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetVideoEncoderConfigurationOptions.xml");
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::createPullPointSubscription(const QString &url, const QString &uuid)
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/CreatePullPointSubscription.xml");
    QDateTime now = QDateTime::currentDateTimeUtc();
    QString format = "yyyy-MM-ddThh:mm:ssZ";
    QString currentTime = now.toString(format);
    QString terminationTime = now.addSecs(60).toString(format);
    body = body.arg(uuid).arg(url).arg(currentTime).arg(terminationTime);
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::renew(const QString &uuid)
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/Renew.xml");
    QDateTime now = QDateTime::currentDateTimeUtc();
    QString format = "yyyy-MM-ddThh:mm:ssZ";
    QString currentTime = now.toString(format);
    QString terminationTime = now.addSecs(60).toString(format);
    body = body.arg(uuid).arg(currentTime).arg(terminationTime);
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::pullMessages()
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/PullMessages.xml");
    QDateTime now = QDateTime::currentDateTimeUtc();
    QString format = "yyyy-MM-ddThh:mm:ssZ";
    QString currentTime = now.toString(format);
    QString terminationTime = now.addSecs(60).toString(format);
    QString utcTime = now.addSecs(-1).toString(format);
    body = body.arg(currentTime).arg(terminationTime).arg(utcTime);
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getSnapshotUri(const QString &url)
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/GetSnapshotUri.xml");
    body = body.arg(url);
    return OnvifListenHelper::getData(body);
}

QByteArray OnvifListenHelper::getFault()
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/Fault.xml");
    return OnvifListenHelper::getData(body, "400 Bad Request");
}

QByteArray OnvifListenHelper::getDefault(const QString &cmd)
{
    QString body = OnvifDeviceHelper::getFile(":/onvifresponse/Default.xml");
    body = body.arg(cmd);
    return OnvifListenHelper::getData(body);
}
```
欢迎关注微信公众号:Qt实战/Qt入门和进阶(各种开源作品、经验整理、项目实战技巧,专注Qt/C++软件开发,视频监控、物联网、工业控制、嵌入式软件、国产化系统应用软件开发) QQ:517216493  WX:feiyangqingyun  QQ群:751439350
快速回复
限100 字节
 
上一个 下一个