一、前言说明
使用过不少的gb28181服务端工具,绝大部分都是BS结构的,也就是直接在网页上运行,比如easynvr、liveqing等,也有个知名的开源国标项目叫wvp,总体感觉性能都不如意,理论上来说肯定不如直接CS结构的可执行文件来解码显示来的快,毕竟网页运行的东西必须完全依赖浏览器,而浏览器的性能在视频解码这块,比起本地解码来还是差很多的,而且很多时候无法用硬解码,还有就是在国产化大潮下,在国产芯片的电脑上运行网页程序,性能非常的不如意,也许若干年后会好很多。
目前市面上没有看到什么监控系统支持gb28181,支持国标的都是服务器上运行,而且基本上和流媒体服务绑定在一起,也就是国标流拉到后直接推给了流媒体服务,然后给出各种拉流地址让监控系统去拉流显示。这种目前是主流的做法。之前就做过onvif版本的视频监控系统,陆陆续续有不少人问过有没有gb28181版本的,当时看了下协议文档,觉得挺复杂的,就暂时搁置了很多年,最近一年刚好有个大项目需要,索性就直接研究摸透一下,用纯qt给整出来了,sip服务支持udp和tcp,拉流支持udp/tcp被动/tcp主动。发布后绿色版开箱即用。
二、效果图
三、相关地址
- 国内站点:https://gitee.com/feiyangqingyun
- 国际站点:https://github.com/feiyangqingyun
- 个人作品:https://blog.csdn.net/feiyangqingyun/article/details/97565652
- 文件地址:https://pan.baidu.com/s/1d7TH_GEYl5nOecuNlWJJ7g 提取码:01jf 文件名:bin_video_gb28181。
四、功能特点
- 支持设备注册、注销、心跳、校时、注册认证、注销认证等。
- 设备上线后可以手动获取设备状态、设备信息、配置信息、预置位信息等。
- 设备上线后自动获取设备通道信息,包括中文通道名称。识别到通道上线离线变化,会重新获取该设备的所有通道信息。
- 支持视频点播,可以分别点播主码流和子码流,内置rtp解包线程,解包后发给视频播放组件解码播放。
- 每个设备每个通道支持点播多个视频,通过ssrc区分,支持共用端口和不同端口收流。
- 支持对某个设备下面所有通道、某个通道、某个通道对应的某个流分别关闭。
- 支持录像文件查询和回放,回放控制支持暂停播放、继续播放、倍速播放、切换播放进度。
- 支持录像文件下载,支持倍速比如8倍速下载,可同时多线程批量下载。
- 回放和下载同时支持IPC和NVR,比如摄像头自带的SD存储卡录像文件回放,NVR上的硬盘录像文件回放。
- 支持云台控制,向上、向下、向左、向右、左上、右上、左下、右下方位移动,镜头放大缩小,光圈放大缩小,镜头聚焦放焦。
- 支持预置位信息的查询、调用、添加、修改、删除等操作。
- 自动目录订阅功能,通道上线下线都有对应的信号通知。
- 内置定时读取通道信息机制,以保证通道信息是最新的,比如有些NVR是不断更新的通道信息。
- 内置订阅警情和位置移动功能,订阅后各种警情事件比如运动目标检测报警、入侵检测报警、徘徊检测报警等自动上报。
- 支持语音对讲功能,可以直接在视频窗体的悬浮条上单击语音对讲按钮,再次单击关闭对讲,对讲期间悬浮条常驻显示。
- 支持设备布防撤防,布防后警情信息会主动上报。
- 国标服务同时支持udp和tcp方式,可选只监听一种或者两种都监听,tcp方式自动处理粘包问题。
- 国标拉流同时支持udp、tcp被动、tcp主动三种方式,每个通道都可以自由选择何种拉流方式。
- 内置拉流端口池,每次拉流从中取出一个,关闭流自动回收端口号,重复利用。
- 收流端口自动纠错,自动跳过被占用的端口,不会出现端口占用导致收流失败的情况。
- 支持三种取流方式自动检测离线重连,检测到离线后,自动重启点播拉流整个流程。
- 录像文件回放,上一个完成后自动切换到下一个继续回放,直到所有回放完成。支持高达8倍速回放。
- 视频播放自适应硬解码,极低资源占用,实时性极好,带悬浮条显示视频流信息,可以直接在悬浮条单击按钮保存录像文件到本地。
- 支持几千路国标消息交互并发,实时视频流支持64路同时显示,可以拓展更多路数。
- 支持阿里云等云服务器,可以分别设置内网监听地址和外网访问地址,一般云服务器上是监听地址用内网,对外访问用外网地址。
- 支持视频分发,也就是推流,视频通道打开后可以自动推流到流媒体服务器,其他需要的地方拉流即可,支持rtsp、rtmp、hls、webrtc等方式拉流。
- 实时预览和录像回放都支持推流,推流支持叠加文字和图片水印以及各种ffmpeg支持的滤镜效果,支持多个水印同时叠加。
- 同时支持gb28181-2011、gb28181-2016、gb28181-2022以及后续可能的所有协议版本。
- SIP解析和交互采用纯Qt底层代码实现,udp/tcp通信交互,祖传原创代码解析,不依赖任何第三方。
- 代码量少,gb28181交互部分共几千行代码,注释详细,接口友好,使用极其简单,提供非常详细的使用示例。
- 支持海康、大华、宇视、华为、天地伟业等所有国标设备,包括一些没有ssrc的设备。
- 支持所有Qt版本和编译器以及操作系统,包括但不限于win、linux、mac、android、嵌入式linux、树莓派香橙派、国产os等。
五、相关代码
#include "gb28181server.h"
#include "gb28181serverudp.h"
#include "gb28181servertcp.h"
GB28181Server::GB28181Server(QObject *parent) : QObject(parent)
{
//注册数据类型
qRegisterMetaType<GB28181Event>("GB28181Event");
qRegisterMetaType<GB28181Status>("GB28181Status");
qRegisterMetaType<GB28181Preset>("GB28181Preset");
qRegisterMetaType<GB28181Record>("GB28181Record");
qRegisterMetaType<GB28181Channel>("GB28181Channel");
qRegisterMetaType<QList<GB28181Preset> >("QList<GB28181Preset>");
qRegisterMetaType<QList<GB28181Record> >("QList<GB28181Record>");
qRegisterMetaType<QList<GB28181Channel> >("QList<GB28181Channel>");
//国标服务udp通信
gbUdpServer = new GB28181ServerUdp;
gbUdpServer->setObjectName("gbUdpServer");
connect(gbUdpServer, SIGNAL(sendData(QString, int, QString, QString)), this, SIGNAL(sendData(QString, int, QString, QString)));
connect(gbUdpServer, SIGNAL(receiveData(QString, int, QString, QString)), this, SIGNAL(receiveData(QString, int, QString, QString)));
connect(gbUdpServer, SIGNAL(receiveInfo(QString, int, QString, QString)), this, SIGNAL(receiveInfo(QString, int, QString, QString)));
connect(gbUdpServer, SIGNAL(startVideo(QString, int, QString, QString, QString, int, int)), this, SIGNAL(startVideo(QString, int, QString, QString, QString, int, int)));
connect(gbUdpServer, SIGNAL(startAudio(QString, int, QString, QString, QString, int, int)), this, SIGNAL(startAudio(QString, int, QString, QString, QString, int, int)));
connect(gbUdpServer, SIGNAL(deviceChanged(QString, bool)), this, SIGNAL(deviceChanged(QString, bool)));
connect(gbUdpServer, SIGNAL(channelChanged(QString, QList<GB28181Channel>)), this, SIGNAL(channelChanged(QString, QList<GB28181Channel>)));
connect(gbUdpServer, SIGNAL(receiveEvent(GB28181Event)), this, SIGNAL(receiveEvent(GB28181Event)));
connect(gbUdpServer, SIGNAL(receiveStatus(GB28181Status)), this, SIGNAL(receiveStatus(GB28181Status)));
connect(gbUdpServer, SIGNAL(receivePosition(GB28181Position)), this, SIGNAL(receivePosition(GB28181Position)));
connect(gbUdpServer, SIGNAL(receivePreset(QList<GB28181Preset>)), this, SIGNAL(receivePreset(QList<GB28181Preset>)));
connect(gbUdpServer, SIGNAL(receiveRecord(QList<GB28181Record>)), this, SIGNAL(receiveRecord(QList<GB28181Record>)));
//国标服务tcp通信
gbTcpServer = new GB28181ServerTcp;
gbTcpServer->setObjectName("gbTcpServer");
connect(gbTcpServer, SIGNAL(sendData(QString, int, QString, QString)), this, SIGNAL(sendData(QString, int, QString, QString)));
connect(gbTcpServer, SIGNAL(receiveData(QString, int, QString, QString)), this, SIGNAL(receiveData(QString, int, QString, QString)));
connect(gbTcpServer, SIGNAL(receiveInfo(QString, int, QString, QString)), this, SIGNAL(receiveInfo(QString, int, QString, QString)));
connect(gbTcpServer, SIGNAL(startVideo(QString, int, QString, QString, QString, int, int)), this, SIGNAL(startVideo(QString, int, QString, QString, QString, int, int)));
connect(gbTcpServer, SIGNAL(startAudio(QString, int, QString, QString, QString, int, int)), this, SIGNAL(startAudio(QString, int, QString, QString, QString, int, int)));
connect(gbTcpServer, SIGNAL(deviceChanged(QString, bool)), this, SIGNAL(deviceChanged(QString, bool)));
connect(gbTcpServer, SIGNAL(channelChanged(QString, QList<GB28181Channel>)), this, SIGNAL(channelChanged(QString, QList<GB28181Channel>)));
connect(gbTcpServer, SIGNAL(receiveEvent(GB28181Event)), this, SIGNAL(receiveEvent(GB28181Event)));
connect(gbTcpServer, SIGNAL(receiveStatus(GB28181Status)), this, SIGNAL(receiveStatus(GB28181Status)));
connect(gbTcpServer, SIGNAL(receivePosition(GB28181Position)), this, SIGNAL(receivePosition(GB28181Position)));
connect(gbTcpServer, SIGNAL(receivePreset(QList<GB28181Preset>)), this, SIGNAL(receivePreset(QList<GB28181Preset>)));
connect(gbTcpServer, SIGNAL(receiveRecord(QList<GB28181Record>)), this, SIGNAL(receiveRecord(QList<GB28181Record>)));
//国标服务udp线程/启动线程后就启动服务/线程关闭则释放对象
gbUdpThread = new QThread;
gbUdpServer->moveToThread(gbUdpThread);
connect(gbUdpThread, SIGNAL(started()), gbUdpServer, SLOT(start()));
connect(gbUdpThread, SIGNAL(finished()), gbUdpServer, SLOT(deleteLater()));
//国标服务tcp线程/启动线程后就启动服务/线程关闭则释放对象
gbTcpThread = new QThread;
gbTcpServer->moveToThread(gbTcpThread);
connect(gbTcpThread, SIGNAL(started()), gbTcpServer, SLOT(start()));
connect(gbTcpThread, SIGNAL(finished()), gbTcpServer, SLOT(deleteLater()));
}
GB28181Server::~GB28181Server()
{
this->stop();
}
void GB28181Server::setServerPara(GB28181ServerPara serverPara)
{
gbUdpServer->setServerPara(serverPara);
gbTcpServer->setServerPara(serverPara);
}
QStringList GB28181Server::getDeviceIps()
{
QStringList ips;
QStringList ips1 = gbUdpServer->getDeviceIps();
QStringList ips2 = gbTcpServer->getDeviceIps();
foreach (QString ip, ips1) {
if (!ips.contains(ip)) {
ips << ip;
}
}
foreach (QString ip, ips2) {
if (!ips.contains(ip)) {
ips << ip;
}
}
return ips;
}
QList<GB28181Device> GB28181Server::getDevices()
{
QList<GB28181Device> devices;
QList<GB28181Device> devices1 = gbUdpServer->getDevices();
QList<GB28181Device> devices2 = gbTcpServer->getDevices();
foreach (GB28181Device device, devices1) {
if (!devices.contains(device)) {
devices << device;
}
}
foreach (GB28181Device device, devices2) {
if (!devices.contains(device)) {
devices << device;
}
}
return devices;
}
void GB28181Server::start(ListenMode listenMode)
{
if (listenMode == ListenMode_Udp) {
gbUdpThread->start();
} else if (listenMode == ListenMode_Tcp) {
gbTcpThread->start();
} else {
gbUdpThread->start();
gbTcpThread->start();
}
}
void GB28181Server::stop()
{
gbUdpThread->quit();
gbTcpThread->quit();
}
void GB28181Server::query(const QString &deviceId, const QString &cmdType)
{
if (gbUdpServer->exist(deviceId)) {
gbUdpServer->query(deviceId, cmdType);
} else {
gbTcpServer->query(deviceId, cmdType);
}
}
void GB28181Server::queryConfig(const QString &deviceId)
{
if (gbUdpServer->exist(deviceId)) {
gbUdpServer->queryConfig(deviceId);
} else {
gbTcpServer->queryConfig(deviceId);
}
}
void GB28181Server::queryPreset(const QString &deviceId, const QString &channelId)
{
if (gbUdpServer->exist(deviceId)) {
gbUdpServer->queryPreset(deviceId, channelId);
} else {
gbTcpServer->queryPreset(deviceId, channelId);
}
}
void GB28181Server::queryRecord(const QString &deviceId, const QString &channelId, const QDateTime &dateStart, const QDateTime &dateEnd)
{
QString startTime = dateStart.toString("yyyy-MM-ddThh:mm:ss");
QString endTime = dateEnd.toString("yyyy-MM-ddThh:mm:ss");
this->queryRecord(deviceId, channelId, startTime, endTime);
}
void GB28181Server::queryRecord(const QString &deviceId, const QString &channelId, const QString &startTime, const QString &endTime)
{
if (gbUdpServer->exist(deviceId)) {
gbUdpServer->queryRecord(deviceId, channelId, startTime, endTime);
} else {
gbTcpServer->queryRecord(deviceId, channelId, startTime, endTime);
}
}
void GB28181Server::subscribe(const QString &deviceId, const QString &channelId)
{
QDateTime now = QDateTime::currentDateTime();
QString startTime = now.toString("yyyy-MM-ddT00:00:00");
QString endTime = now.toString("yyyy-MM-ddT23:59:59");
this->subscribe(deviceId, channelId, "Alarm", 3600, startTime, endTime);
}
void GB28181Server::subscribe(const QString &deviceId, const QString &channelId, const QString &cmdType, int expires, const QString &startTime, const QString &endTime)
{
if (gbUdpServer->exist(deviceId)) {
gbUdpServer->subscribe(deviceId, channelId, cmdType, expires, startTime, endTime);
} else {
gbTcpServer->subscribe(deviceId, channelId, cmdType, expires, startTime, endTime);
}
}
void GB28181Server::broadcast(const QString &deviceId, const QString &channelId, int audioPort)
{
if (gbUdpServer->exist(deviceId)) {
gbUdpServer->broadcast(deviceId, channelId, audioPort);
} else {
gbTcpServer->broadcast(deviceId, channelId, audioPort);
}
}
void GB28181Server::ptz(const QString &deviceId, const QString &channelId, PtzType type, quint8 speed)
{
if (gbUdpServer->exist(deviceId)) {
gbUdpServer->ptz(deviceId, channelId, type, speed);
} else {
gbTcpServer->ptz(deviceId, channelId, type, speed);
}
}
void GB28181Server::guard(const QString &deviceId, const QString &channelId, bool arming)
{
if (gbUdpServer->exist(deviceId)) {
gbUdpServer->guard(deviceId, channelId, arming);
} else {
gbTcpServer->guard(deviceId, channelId, arming);
}
}
QString GB28181Server::invite(const QString &deviceId, const QString &channelId, int videoPort, const QString &startTime, const QString &endTime, int speed, int profile, TransmitMode mode, bool playback)
{
if (gbUdpServer->exist(deviceId)) {
return gbUdpServer->invite(deviceId, channelId, videoPort, startTime, endTime, speed, profile, mode, playback);
} else {
return gbTcpServer->invite(deviceId, channelId, videoPort, startTime, endTime, speed, profile, mode, playback);
}
}
void GB28181Server::bye(const QString &deviceId, const QString &channelId, const QString &ssrc)
{
if (gbUdpServer->exist(deviceId)) {
gbUdpServer->bye(deviceId, channelId, ssrc);
} else {
gbTcpServer->bye(deviceId, channelId, ssrc);
}
}
void GB28181Server::playControl(const QString &deviceId, const QString &channelId, PlayControl control, float value, const QString &ssrc)
{
if (gbUdpServer->exist(deviceId)) {
gbUdpServer->playControl(deviceId, channelId, control, value, ssrc);
} else {
gbTcpServer->playControl(deviceId, channelId, control, value, ssrc);
}
}