一、前言說明
一個好的視頻監控系統,設備掉線后能夠自動重連,也是一個重要的功能指標,如果監控系統只是個rtsp流地址,那非常好辦,只需要重新打開流地址即可,而gb28181中就變得復雜了很多,需要多個方便配合,主要涉及到sip交互指令,rtp解包,ffmpeg解碼,三個方面缺一不可,其中sip這邊負責讓設備發流數據,而且有多個交互指令,rtp這邊也需要先綁定地址監聽端口成功后,再把端口號一起sdp應答給設備,sip和rtp都完成后再打開ffmpeg準備收數據解碼,三個方面都要配合,只要有一個方面配合的不好就會失敗,關鍵是取流還支持三種方式,udp,tcp被動,tcp主動,三種方式都要能支持自動重連。網上很多國標的工具,幾乎都沒有這種重連的功能。
在做這個重連功能中,有幾個要點需要注意,一個是當用戶主動關閉了流,是不需要加入重連檢測的,不然永遠都關不掉,這個可以加個標志位來解決,當用戶打開流的時候標志位真,用戶關閉流的時候標志位假,重連定時器判斷標志位,為真才需要去檢測。還一個是資源的釋放,重連過程中必須先把之前的rtp解包線程的資源釋放,還有ffmpeg解碼這邊的資源全部釋放,不然一直累加會導致內存泄漏。
二、效果圖
三、相關地址
- 國內站點: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 "rtphelper.h"
#include "rtpthreadreceive.h"
#include "rtpthreadsend.h"
#include "gb28181widget.h"
#include "gb28181widgetmanage.h"
#include "gb28181server.h"
#include "urlhelper.h"bool GB28181Widget::checkSsrc = true;
GB28181Widget::GB28181Widget(QWidget *parent) : VideoWidget(parent)
{server = NULL;this->speed = 1;this->profile = 1;this->mode = TransmitMode_UdpServer;//創建視頻解碼線程rtpVideo = new RtpThreadReceive;rtpVideo->setProperty("running", false);connect(rtpVideo, SIGNAL(openOk(int)), this, SLOT(openOk(int)));connect(rtpVideo, SIGNAL(receiveData(QByteArray, qint64)), this, SLOT(receiveData(QByteArray, qint64)));//創建音頻發送線程rtpAudio = new RtpThreadSend;connect(rtpAudio, SIGNAL(openOk(int)), this, SLOT(openOk(int)));//關聯視頻窗體信號槽connect(this, SIGNAL(sig_receivePlayStart(int)), this, SLOT(receivePlayStartx(int)));connect(this, SIGNAL(sig_receivePlayFinsh()), this, SLOT(receivePlayFinshx()));
}void GB28181Widget::initPara(int scaleMode, int videoMode, int videoCore, int decodeType, const QString &hardware, int readTimeout, bool call, bool download)
{GB28181WidgetManage::initPara(this, scaleMode, videoMode, videoCore, decodeType, hardware, readTimeout, call, download);
}void GB28181Widget::clearPara()
{this->ssrcVideo = "";this->ssrcAudio = "";this->deviceId = "";this->channelId = "";this->startTime = "";this->endTime = "";
}qint64 GB28181Widget::getPts() const
{return rtpVideo->getPts();
}RtpThreadReceive *GB28181Widget::getThread() const
{return this->rtpVideo;
}QString GB28181Widget::getSsrcVideo() const
{return this->ssrcVideo;
}QString GB28181Widget::getSsrcAudio() const
{return this->ssrcAudio;
}QString GB28181Widget::getDeviceId() const
{return this->deviceId;
}QString GB28181Widget::getChannelId() const
{return this->channelId;
}void GB28181Widget::setServer(GB28181Server *server)
{this->server = server;connect(server, SIGNAL(startVideo(QString, int, QString, QString, QString, int, int)), this, SLOT(startVideo(QString, int, QString, QString, QString, int, int)), Qt::UniqueConnection);connect(server, SIGNAL(startAudio(QString, int, QString, QString, QString, int, int)), this, SLOT(startAudio(QString, int, QString, QString, QString, int, int)), Qt::UniqueConnection);
}void GB28181Widget::setPara(float speed, int profile, TransmitMode mode, const QString &serverIp, const QString &pushUrl, PlayType playType)
{this->speed = speed;this->profile = profile;this->mode = mode;this->serverIp = serverIp;this->pushUrl = pushUrl;this->playType = playType;this->setSpeed(speed);
}void GB28181Widget::setTime(const QString &startTime, const QString &endTime)
{this->startTime = startTime;this->endTime = endTime;
}void GB28181Widget::openVideo(const QString &deviceId, const QString &channelId)
{this->deviceId = deviceId;this->channelId = channelId;//啟動階段先要停止if (rtpVideo->isRunning()) {rtpVideo->stop();}//從端口池中取出一個端口lastPort = RtpHelper::takePort();rtpVideo->setProperty("running", true);rtpVideo->setPara(serverIp, lastPort, serverIp, lastPort, mode);rtpVideo->start();
}bool GB28181Widget::closeVideo(const QString &deviceId, const QString &channelId, const QString &ssrc)
{if (this->deviceId != deviceId) {return false;}if (!channelId.isEmpty() && this->channelId != channelId) {return false;}if (!ssrc.isEmpty() && this->ssrcVideo != ssrc) {return false;}this->stop();return true;
}void GB28181Widget::appendAudio(const QByteArray &data)
{rtpAudio->append(data);
}void GB28181Widget::openAudio(const QString &deviceId, const QString &channelId)
{//啟動階段先要停止if (rtpAudio->isRunning()) {rtpAudio->stop();}//從端口池中取出一個端口lastPort = RtpHelper::takePort();rtpAudio->setPara(serverIp, lastPort, serverIp, lastPort, mode);rtpAudio->start();
}bool GB28181Widget::closeAudio(const QString &deviceId, const QString &channelId, const QString &ssrc)
{if (this->deviceId != deviceId) {return false;}if (!channelId.isEmpty() && this->channelId != channelId) {return false;}if (!ssrc.isEmpty() && this->ssrcAudio != ssrc) {return false;}//停止語音對講和發流線程server->bye(deviceId, channelId, ssrcAudio);rtpAudio->stop();return true;
}void GB28181Widget::closeEvent(QCloseEvent *)
{this->stop();
}void GB28181Widget::openOk(int port)
{//收流端口打開成功才能繼續點播lastPort = port;if (sender() == rtpVideo) {bool playback = (playType == PlayType_Playback);ssrcVideo = server->invite(deviceId, channelId, port, startTime, endTime, speed, profile, mode, playback);} else {server->broadcast(deviceId, channelId, port);}
}void GB28181Widget::receiveData(const QByteArray &data, qint64 ssrc)
{if (videoThread) {
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))videoThread->appendData(data);
#elseQMetaObject::invokeMethod(videoThread, "appendData", Q_ARG(QByteArray, data));
#endif}
}void GB28181Widget::startVideo(const QString &host, int port, const QString &deviceId, const QString &channelId, const QString &ssrc, int transmitMode, int transmitPort)
{//過濾下是不是當前窗體if (this->deviceId != deviceId || this->channelId != channelId || transmitMode != mode) {return;}//有些設備并沒有按照下發的ssrc作為標識/而是自己定義的/這里就需要去掉這個判斷if (checkSsrc && this->ssrcVideo != ssrc) {return;}//qDebug() << TIMEMS << "startVideo" << mode << host << port << deviceId << channelId << ssrc << transmitMode << transmitPort;if (mode == TransmitMode_TcpClient) {rtpVideo->setPara(serverIp, lastPort, host, transmitPort, mode);rtpVideo->setOk(true);}//打開視頻接收流數據/設置是否禁用解碼/僅推流不需要解碼/極大減少資源占用if (!this->getIsRunning()) {this->open(QString("stream://%1_%2").arg(deviceId).arg(channelId));this->getVideoThread()->setDisableDecode(this->property("disableDecode").toBool());}
}void GB28181Widget::startAudio(const QString &host, int port, const QString &deviceId, const QString &channelId, const QString &ssrc, int transmitMode, int transmitPort)
{//過濾下是不是當前窗體if (this->deviceId != deviceId || this->channelId != channelId) {return;}ssrcAudio = ssrc;rtpAudio->setPara(serverIp, lastPort, host, transmitPort, transmitMode, ssrc);rtpAudio->setOk(true);
}void GB28181Widget::receivePlayStartx(int)
{//設置播放速度this->setSpeed(speed);//執行保存文件或推流if (playType == PlayType_Download) {QString date = startTime.left(10);QString start = startTime.mid(11).replace(":", "-");QString end = endTime.mid(11).replace(":", "-");QString name = QString("%1_%2_%3_%4_%5.mp4").arg(deviceId).arg(channelId).arg(date).arg(start).arg(end);this->getVideoThread()->recordStart(qApp->applicationDirPath() + "/video/" + name);} else if (!pushUrl.isEmpty()) {QString url = this->getVideoPara().mediaUrl.mid(9);url = pushUrl + "/" + url + "_" + ssrcVideo;this->getVideoThread()->recordStart(url);//要轉成外網地址才能正常訪問QString ip = UrlHelper::getUrlIP(url);GB28181ServerPara para = server->getServerPara();url.replace(ip, para.serverHost);emit pushStart(this, url);}
}void GB28181Widget::receivePlayFinshx()
{//判斷唯一標識/一個通道可能打開了多個流/只需要關閉當前窗體對應的流if (!ssrcVideo.isEmpty()) {rtpVideo->setProperty("running", false);server->bye(deviceId, channelId, ssrcVideo);rtpVideo->stop();}if (!ssrcAudio.isEmpty()) {server->bye(deviceId, channelId, ssrcAudio);rtpAudio->stop();}if (!deviceId.isEmpty() && !channelId.isEmpty()) {emit pushStop(deviceId, channelId);}this->clearPara();
}