把通用的视频控件搞定以后,后期增加新的内核方便多了,不需要在好多个文件复制粘贴之类的,接下来就是需要一个统一的类来管理视频监控系统中的16个通道或者32个通道,甚至64个通道也有可能,当然,通用通道管理也兼容各种监控内核,以前通道管理类,是每个内核写一个,也是很繁琐,大量的重复性代码,所以将通用视频监控控件整理好以后,顺其自然的要改造这个通用通道管理的类了。
通用通道管理的需求来源自实际的开发过程需要,比如断线重连机制,尽管每个视频监控控件自带了断线重连功能,很容易会出现极端的情况,比如网络断了以后,设备重新上线,会全部瞬间重新上线(如果设置的超时时间一致的话),这就给CPU造成很大压力,瞬间暴增,所以需要一个类专门管理所有的摄像机设备,由他来负责排队断线重连,加载打开设备,统一的截图机制,统一的视频存储机制。
通道管理基本功能:
#include "commonvideomanage.h"
#ifdef videovlc
#include "vlchelper.h"
#elif videoffmpeg
#include "ffmpeghelper.h"
#elif haikang
#include "haikanghelper.h"
#endif
#define TIMEMS qPrintable(QTime::currentTime().toString("HH:mm:ss zzz"))
#define TIME qPrintable(QTime::currentTime().toString("HH:mm:ss"))
#define QDATE qPrintable(QDate::currentDate().toString("yyyy-MM-dd"))
#define QTIME qPrintable(QTime::currentTime().toString("HH-mm-ss"))
#define DATETIME qPrintable(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
#define STRDATETIME qPrintable(QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss"))
#define STRDATETIMEMS qPrintable(QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss-zzz"))
QScopedPointer<CommonVideoManage> CommonVideoManage::self;
CommonVideoManage *CommonVideoManage::Instance()
{
if (self.isNull()) {
static QMutex mutex;
QMutexLocker locker(&mutex);
if (self.isNull()) {
self.reset(new CommonVideoManage);
}
}
return self.data();
}
CommonVideoManage::CommonVideoManage(QObject *parent) : QObject(parent)
{
timeout = 10;
openInterval = 1000;
checkInterval = 5;
videoCount = 16;
saveVideo = false;
saveVideoInterval = 0;
savePath = qApp->applicationDirPath();
//打开视频定时器
timerOpen = new QTimer(this);
connect(timerOpen, SIGNAL(timeout()), this, SLOT(openVideo()));
timerOpen->setInterval(openInterval);
//重连视频定时器
timerCheck = new QTimer(this);
connect(timerCheck, SIGNAL(timeout()), this, SLOT(checkVideo()));
timerCheck->setInterval(checkInterval * 1000);
//新建目录
newDir("snap");
}
CommonVideoManage::~CommonVideoManage()
{
this->stop();
}
QString CommonVideoManage::getVersion2()
{
#if (defined videovlc) || (defined videoffmpeg) || (defined haikang)
return getVersion();
#else
return "1.0";
#endif
}
void CommonVideoManage::newDir(const QString &dirName)
{
//如果路径中包含斜杠字符则说明是绝对路径
//linux系统路径字符带有 / windows系统 路径字符带有 :/
QString strDir = dirName;
if (!strDir.startsWith("/") && !strDir.contains(":/")) {
strDir = QString("%1/%2").arg(qApp->applicationDirPath()).arg(strDir);
}
QDir dir(strDir);
if (!dir.exists()) {
dir.mkpath(strDir);
}
}
void CommonVideoManage::openVideo()
{
if (index < videoCount) {
//取出一个进行打开,跳过为空的立即下一个
QString url = videoUrls.at(index);
if (!url.isEmpty()) {
this->open(index);
index++;
} else {
index++;
this->openVideo();
}
} else {
//全部取完则关闭定时器
timerOpen->stop();
}
}
void CommonVideoManage::checkVideo()
{
//如果打开定时器还在工作则不用继续
if (timerOpen->isActive()) {
return;
}
QDateTime now = QDateTime::currentDateTime();
for (int i = 0; i < videoCount; i++) {
//只有url不为空的才需要处理重连
if (videoUrls.at(i).isEmpty()) {
continue;
}
//如果10秒内已经处理过重连则跳过当前这个,防止多个掉线一直处理第一个掉线的
if (lastTimes.at(i).secsTo(now) < 10) {
continue;
}
//计算超时时间
QDateTime lastTime = videoWidgets.at(i)->getLastTime();
int sec = lastTime.secsTo(now);
if (sec >= timeout) {
//重连该设备
videoWidgets.at(i)->restart();
//每次重连记住最后重连时间
lastTimes[i] = now;
//break;
}
}
}
void CommonVideoManage::snapImage(const QImage &image)
{
CommonVideoWidget *w = (CommonVideoWidget *)sender();
QString fileName = w->property("fileName").toString();
if (!image.isNull()) {
image.save(fileName, "jpg");
}
}
void CommonVideoManage::setTimeout(int timeout)
{
if (timeout >= 5 && timeout < 60) {
this->timeout = timeout;
}
}
void CommonVideoManage::setOpenInterval(int openInterval)
{
if (openInterval >= 0 && openInterval <= 2000) {
this->openInterval = openInterval;
timerOpen->setInterval(openInterval);
}
}
void CommonVideoManage::setCheckInterval(int checkInterval)
{
if (checkInterval >= 5 && checkInterval <= 60) {
this->checkInterval = checkInterval;
timerCheck->setInterval(checkInterval * 1000);
}
}
void CommonVideoManage::setVideoCount(int videoCount)
{
this->videoCount = videoCount;
}
void CommonVideoManage::setSaveVideo(bool saveVideo)
{
this->saveVideo = saveVideo;
}
void CommonVideoManage::setSaveVideoInterval(int saveVideoInterval)
{
this->saveVideoInterval = saveVideoInterval;
}
void CommonVideoManage::setSavePath(const QString &savePath)
{
this->savePath = savePath;
}
void CommonVideoManage::setUrls(const QList<QString> &videoUrls)
{
this->videoUrls = videoUrls;
}
void CommonVideoManage::setNames(const QList<QString> &videoNames)
{
this->videoNames = videoNames;
}
void CommonVideoManage::setWidgets(QList<CommonVideoWidget *> videoWidgets)
{
this->videoWidgets = videoWidgets;
}
void CommonVideoManage::start()
{
if (videoWidgets.count() != videoCount) {
return;
}
lastTimes.clear();
for (int i = 0; i < videoCount; i++) {
lastTimes.append(QDateTime::currentDateTime());
QString url = videoUrls.at(i);
if (!url.isEmpty()) {
CommonVideoWidget *w = videoWidgets.at(i);
#ifdef videoffmpeg
disconnect(w, SIGNAL(snapImage(QImage)), this, SLOT(snapImage(QImage)));
connect(w, SIGNAL(snapImage(QImage)), this, SLOT(snapImage(QImage)));
#endif
//设置文件url地址
w->setUrl(url);
//如果是USB摄像头则单独设置宽高
if (w->getIsUsbCamera()) {
w->setVideoWidth(640);
w->setVideoHeight(480);
}
//设置OSD信息,可见+字体大小+文字+颜色+格式+位置
if (i < videoNames.count()) {
w->setOSD1Visible(true);
w->setOSD1FontSize(18);
w->setOSD1Text(videoNames.at(i));
w->setOSD1Color(Qt::yellow);
w->setOSD1Format(CommonVideoWidget::OSDFormat_Text);
w->setOSD1Position(CommonVideoWidget::OSDPosition_Right_Top);
//还可以设置第二路OSD
#if 0
w->setOSD2Visible(true);
w->setOSD2FontSize(18);
w->setOSD2Color(Qt::yellow);
w->setOSD2Format(CommonVideoWidget::OSDFormat_DateTime);
w->setOSD2Position(CommonVideoWidget::OSDPosition_Left_Bottom);
#endif
}
//设置是否存储文件
w->setSaveFile(saveVideo);
w->setSavePath(savePath);
w->setSaveInterval(saveVideoInterval);
if (saveVideo && saveVideoInterval == 0) {
QString path = QString("%1/%2").arg(savePath).arg(QDATE);
newDir(path);
QString fileName = QString("%1/Ch%2_%3.mp4").arg(path).arg(i + 1).arg(STRDATETIME);
w->setFileName(fileName);
}
//打开间隔 = 0 毫秒则立即打开
if (openInterval == 0) {
this->open(i);
}
}
}
//启动定时器挨个排队打开
if (openInterval > 0) {
index = 0;
timerOpen->start();
}
//启动定时器排队处理重连
QTimer::singleShot(5000, timerCheck, SLOT(start()));
}
void CommonVideoManage::stop()
{
if (videoWidgets.count() != videoCount) {
return;
}
if (timerOpen->isActive()) {
timerOpen->stop();
}
if (timerCheck->isActive()) {
timerCheck->stop();
}
for (int i = 0; i < videoCount; i++) {
this->close(i);
}
}
void CommonVideoManage::open(int index)
{
if (!videoUrls.at(index).isEmpty()) {
videoWidgets.at(index)->open();
}
}
void CommonVideoManage::close(int index)
{
if (!videoUrls.at(index).isEmpty()) {
videoWidgets.at(index)->close();
}
}
void CommonVideoManage::snap(int index, const QString &fileName)
{
if (videoUrls.at(index).isEmpty()) {
return;
}
#ifdef videoffmpeg
CommonVideoWidget *w = videoWidgets.at(index);
w->setProperty("fileName", fileName);
QImage img = w->getImage();
if (!img.isNull()) {
img.save(fileName, "jpg");
}
#else
videoWidgets.at(index)->snap(fileName);
#endif
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。