摘要:在gb28181-2011协议中,只有udp要求,从2016版本开始要求支持tcp,估计也是在多年的实际运行过程中,发现有些网络环境差的场景下,一些udp交互指令丢失导致功能异常,所以后面修订的时候增加了tcp的要求,这个有没有必要呢,我觉得很有必要,而且无论
在gb28181-2011协议中,只有udp要求,从2016版本开始要求支持tcp,估计也是在多年的实际运行过程中,发现有些网络环境差的场景下,一些udp交互指令丢失导致功能异常,所以后面修订的时候增加了tcp的要求,这个有没有必要呢,我觉得很有必要,而且无论是设备端还是服务端,都要求能支持udp和tcp,目前发现很多设备都是默认同时开启了udp和tcp,优先走udp,也可以在后台设置优先走tcp,而服务端这边一般是默认同时监听udp和tcp,无论哪个通道过来的数据,都能正常解析和交互,为此c++强大的继承功能就可以上场了,搞个GB28181ServerBase基类只负责解析数据,同时提供一个writedata虚函数,子类负责具体的发送数据。然后创建一个子类GB28181ServerUdp专门处理收发udp通道的数据,GB28181ServerTcp专门处理收发tcp通道的数据,打完收工非常完美。
这里要特别注意的是,tcp方式的数据会粘包,一般是在录像文件查询返回的数据会出现,因为可能录像文件会有很多个,甚至上百个,会拆分多个xml数据返回,接收数据这边上一个包可能只接收到xml数据的一部分,并不是完整的,要等待一个完整的包收完才能继续解析,如果使用exosip这种开源库,也是要自己处理粘包问题的,一些第三方的国标工具并没有处理这个细节,导致体验很不好,比如录像文件返回的时候会丢掉很多。
一个tcp返回的数据包
09:45:05 358 "MESSAGE sip:34020000002000000001@3402000000 SIP/2.0\r\nVia: SIP/2.0/TCP 192.168.0.64:36031;rport;branch=z9hG4bK519675539\r\nFrom: ;tag=2132572361\r\nTo: \r\nCall-ID: 90473654\r\nCSeq: 20 MESSAGE\r\nContent-Type: Application/MANSCDP+xml\r\nMax-Forwards: 70\r\nUser-Agent: IP Camera\r\nContent-Length: 3159\r\n\r\n\n\nRecordInfo\n10003\n34020000001310000001\nhikangname\n180\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T07:59:40\n2025-05-14T08:00:09\n0\ntime\n7189412\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T13:06:50\n2025-05-14T13:24:10\n0\ntime\n262553600\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T13:24:10\n2025-05-14T13:41:28\n0\ntime\n262160384\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T13:41:28\n2025-05-14T13:58:48\n0\ntime\n262586368\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T13:58:48\n2025-05-14T14:16:10\n0\ntime\n263110656\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T14:16:10\n2025-05-14T14:33:30\n0\ntime\n262619136\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T14:33:30\n2025-05-14T14:50:50\n0\ntime\n262668288\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T14:50:50\n2025-05-14T15:08:09\n0\ntime\n262504448\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T15:08:09\n2025-05-14T15:25:29\n0\ntime\n262832128\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T15:25:29\n2025-05-14T15:42:48\n0\ntime\n262815744\n\n\n\nMESSAGE sip:34020000002000000001@3402000000 SIP/2.0\r\nVia: SIP/2.0/TCP 192.168.0.64:36031;rport;branch=z9hG4bK2125502802\r\nFrom: ;tag=1975167163\r\nTo: \r\nCall-ID: 680663226\r\nCSeq: 20 MESSAGE\r\nContent-Type: Application/MANSCDP+xml\r\nMax-Forwards: 70\r\nUser-Agent: IP Camera\r\nContent-Length: 3160\r\n\r\n\n\nRecordInfo\n10003\n34020000001310000001\nhikangname\n180\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T15:42:48\n2025-05-14T16:00:07\n0\ntime\n262717440\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T16:00:07\n2025-05-14T16:17:27\n0\ntime\n262881280\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T16:17:27\n2025-05-14T16:34:45\n0\ntime\n262193152\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T16:34:45\n2025-05-14T16:52:05\n0\ntime\n262717440\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T16:52:05\n2025-05-14T17:09:25\n0\ntime\n262602752\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T17:09:25\n2025-05-14T17:26:43\n0\ntime\n262324224\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-14T17:26:43\n2025-05-14T17:34:37\n0\ntime\n119819088\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-15T09:09:44\n2025-05-15T09:18:43\n0\ntime\n135921664\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-15T09:10:36\n2025-05-15T09:11:30\n0\ntime\n14242208\n\n\n34020000001310000001\nhikangname\nfile_path\nAddress 1\n2025-05-15T09:18:43\n2025-05-15T09:36:01\n0\ntime\n262553600\n\n\n\n"
支持设备注册、注销、心跳、校时、注册认证、注销认证等。设备上线后可以手动获取设备状态、设备信息、配置信息、预置位信息等。设备上线后自动获取设备通道信息,包括中文通道名称。识别到通道上线离线变化,会重新获取该设备的所有通道信息。支持视频点播,可以分别点播主码流和子码流,内置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 "gb28181serverudp.h"#include "gb28181helper.h"GB28181ServerUdp::GB28181ServerUdp(QObject *parent) : GB28181ServerBase(parent){udpSocket = NULL;}GB28181ServerUdp::~GB28181ServerUdp{this->stop;}void GB28181ServerUdp::readData{//取出收到的数据QMutexLocker locker(&mutex);while (udpSocket->hasPendingDatagrams) {QByteArray data;data.resize(udpSocket->pendingDatagramSize);QHostAddress host;quint16 port;udpSocket->readDatagram(data.data, data.size, &host, &port);if (data.size > 4) {this->parseData(host.toString, port, GB28181Helper::getData(data));}}}void GB28181ServerUdp::writeData(const QString &host, quint16 port, const QString &id, const QString &data){if (data.isEmpty) {return;}udpSocket->writeDatagram(data.toUtf8, QHostAddress(host), port);udpSocket->flush;emit sendData(host, port, id, data);}void GB28181ServerUdp::start{//创建通信对象udpSocket = new QUdpSocket(this);connect(udpSocket, SIGNAL(readyRead), this, SLOT(readData));bool ok = udpSocket->bind(QHostAddress(serverPara.serverIp), serverPara.serverPort);if (!ok) {emit receiveInfo("", 0, "", "UDP端口监听失败");}GB28181ServerBase::start;}void GB28181ServerUdp::stop{udpSocket->deleteLater;GB28181ServerBase::stop;}#include "gb28181servertcp.h"#include "gb28181helper.h"GB28181ServerTcp::GB28181ServerTcp(QObject *parent) : GB28181ServerBase(parent){qRegisterMetaType("QTcpSocket *");//tcp通信会出现粘包/需要收完数据再去处理timerTake = new QTimer(this);connect(timerTake, SIGNAL(timeout), this, SLOT(takeData));timerTake->setInterval(100);}GB28181ServerTcp::~GB28181ServerTcp{this->stop;}void GB28181ServerTcp::newConnection{//收到新的连接QMutexLocker locker(&mutex);QTcpSocket *socket = tcpServer->nextPendingConnection;connect(socket, SIGNAL(readyRead), this, SLOT(readData));connect(socket, SIGNAL(disconnected), this, SLOT(disconnected));sockets deleteLater;}void GB28181ServerTcp::readData{//取出收到的数据QMutexLocker locker(&mutex);QTcpSocket *socket = (QTcpSocket *)sender;QByteArray data = socket->readAll;if (data.size > 4) {int index = sockets.indexOf(socket);datas[index].append(data);times[index] = QDateTime::currentDateTime;}}void GB28181ServerTcp::takeData{QMutexLocker locker(&mutex);for (int i = 0; i peerAddress.toString;quint16 port = socket->peerPort;QString buffer = GB28181Helper::getData(data);//this->parseData(host, port, buffer);continue;//录像文件返回有多个挤在一块/需要单独处理if (data.startsWith("MESSAGE sip:")) {QStringList temp = buffer.split("MESSAGE sip:");for (int j = 1; j takeData(host, port, buffers);continue;}//主动填充分隔符/方便下面分割判断和组合static QString splite = "\r\n\r\n";buffer.replace("\n", "" + splite);//tcp通信存在粘包/需要主动分包QStringList list = buffer.split(splite);int count = list.count;for (int j = 0; j takeData(host, port, buffers);}}void GB28181ServerTcp::takeData(const QString &host, quint16 port, const QStringList &datas){//qDebug parseData(host, port, data);}}void GB28181ServerTcp::writeData(QTcpSocket *socket, const QString &data){socket->write(data.toUtf8);socket->flush;}void GB28181ServerTcp::writeData(const QString &host, quint16 port, const QString &id, const QString &data){if (data.isEmpty) {return;}//找到对应的连接发送数据foreach (QTcpSocket *socket, sockets) {if (socket->peerAddress.toString == host && socket->peerPort == port) {QMetaObject::invokeMethod(this, "writeData", Q_ARG(QTcpSocket *, socket), Q_ARG(QString, data));emit sendData(host, port, id, data);break;}}}void GB28181ServerTcp::start{timerTake->start;tcpServer = new QTcpServer(this);connect(tcpServer, SIGNAL(newConnection), this, SLOT(newConnection));bool ok = tcpServer->listen(QHostAddress(serverPara.serverIp), serverPara.serverPort);if (!ok) {emit receiveInfo("", 0, "", "TCP端口监听失败");}GB28181ServerBase::start;}void GB28181ServerTcp::stop{//先断开所有连接再关闭foreach (QTcpSocket *socket, sockets) {socket->disconnectFromHost;}sockets.clear;datas.clear;times.clear;tcpServer->deleteLater;GB28181ServerBase::stop;} 来源:Qt自定义控件
免责声明:本站系转载,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请在30日内与本站联系,我们将在第一时间删除内容!