

























以前视频监控系统就做了双击打开分组的功能,但是现场也好和本地实际测试也好,都没有出现过问题,而近期在一个集团中测试下来,在双击打开分组的时候很容易蹦,一看每次都是64路同时打开,经常详细测试,发现问题出在两个现象才会发生,一个是分组中有部分通道是离线的,一个是上一个分组还没有全部打开完成,下一个双击触发又开始了,这两者杂交,必触发崩溃,核心就是锁资源竞争了,调用关闭和打开并不是在同一个线程完成的,于是把双击分组的打开也放到videomanage线程处理,其实内部是排队打开,默认间隔0.1s,也可以是0.01s,间隔越短,瞬间占用的CPU越大,如果电脑配置比较好,设置成0也可以可以的,也就是64个通道同时瞬间打开。
最关键的是要在下次分组打开的时候,判断下是不是上一个分组还没有打开完成,如果没有打开完成,就不用继续,排队打开的时候会记住一个索引,当前打开到哪个通道,如果到了末尾说明全部打开完成,这样再去关闭所有通道再打开下一个分组的视频,就可以了。注意这里的打开完成,并不是要画面都出来,因为通道可能离线,而是采集线程开始采集就可以,无需等待采集到画面。经过这几个改进之后,无论怎么双击怎么多少个通道怎么有离线夹杂其中,都不会再崩溃了。顺带把轮询中的打开也改成了视频管理线程去排队打开,效果非常的棒。离完美又更近了一步。



#include "videomanage.h"
#include "abstractsavethread.h"
#include "abstractvideothread.h"
QScopedPointer<VideoManage> VideoManage::self;
VideoManage *VideoManage::Instance()
{
if (self.isNull()) {
static QMutex mutex;
QMutexLocker locker(&mutex);
if (self.isNull()) {
self.reset(new VideoManage);
}
}
return self.data();
}
VideoManage::VideoManage(QObject *parent) : QThread(parent)
{
stopped = false;
openIndex = -1;
openInterval = 100;
saveVideo = true;
recordPath = "./video_normal";
snapPath = "./image_normal";
this->initPath();
//由于解码线程中的打印信息比较多/可以临时关闭打印信息/方便查看其他打印信息
//AbstractSaveThread::debugInfo = 0;
//AbstractVideoThread::debugInfo = 0;
}
VideoManage::~VideoManage()
{
this->stop();
}
bool VideoManage::isEmpty()
{
bool empty = true;
foreach (QString url, mediaUrls) {
if (!url.isEmpty()) {
empty = false;
break;
}
}
return empty;
}
bool VideoManage::isOpenFinsh()
{
if (this->isEmpty()) {
return true;
} else {
return this->openIndex >= videoWidgets.count();
}
}
void VideoManage::run()
{
//每次复位表示从头开始
openIndex = -1;
//默认时间搞个早期时间保证每次都是正确计算
lastRecordTime = QDateTime::fromString("1970-01-01 00:00:00", "yyyy-MM-dd hh:mm:ss");
//校验下数量是否一致
int count1 = mediaUrls.count();
int count2 = videoWidgets.count();
int count3 = recordTimes.count();
if (count1 == 0 || count2 == 0 || count3 == 0 || count1 != count2 || count2 != count3) {
stopped = false;
return;
}
//校验有没有地址
if (this->isEmpty()) {
stopped = false;
return;
}
//线程中不断的打开画面或者校验录像文件存储
while (!stopped) {
this->openVideo();
this->checkRecord();
msleep(1);
}
stopped = false;
}
QList<VideoWidget *> VideoManage::getVideoWidgets()
{
return this->videoWidgets;
}
VideoWidget *VideoManage::getVideoWidget(const QString &rtspMain, const QString &rtspSub)
{
foreach (VideoWidget *videoWidget, videoWidgets) {
QString url = videoWidget->getVideoPara().mediaUrl;
if (url == rtspMain || url == rtspSub) {
return videoWidget;
}
}
return 0;
}
void VideoManage::openVideo()
{
openIndex++;
if (openIndex < videoWidgets.count()) {
//判断是否到末尾发送打开完成信号
if (openIndex == videoWidgets.count() - 1) {
emit openFinsh();
//qDebug() << TIMEMS << "全部打开完成";
}
//立即跳过地址为空的
QString url = mediaUrls.at(openIndex);
if (url.isEmpty()) {
return;
}
//异步执行打开
VideoWidget *videoWidget = videoWidgets.at(openIndex);
QMetaObject::invokeMethod(videoWidget, "open", Q_ARG(QString, url));
msleep(openInterval);
}
}
void VideoManage::initPath()
{
//默认录像文件和截图文件存放路径
//在线程中会实时更新/不用担心日期变了目录没变
VideoWidget::recordPath = recordPath + "/" + QDATE;
VideoWidget::snapPath = snapPath + "/" + QDATE;
}
void VideoManage::doRecord(VideoWidget *videoWidget, const QDateTime &dateTime)
{
//运行阶段才能开启录像
if (!videoWidget->getIsRunning() || !saveVideo) {
return;
}
//取出星期几/默认星期一对应值是1而不是0/所有这里要减去1
int recordWeek = dateTime.date().dayOfWeek() - 1;
//取出当前时间对应录像计划一天中所在的格子
int hour = dateTime.time().hour();
int min = dateTime.time().minute();
int recordIndex = (hour * 2) + (min < 30 ? 0 : 1);
//qDebug() << TIMEMS << "录像计划" << videoWidget->getWidgetPara().videoFlag << recordWeek << recordIndex;
//设置录像文件格式
videoWidget->getVideoThread()->setSaveVideoType(SaveVideoType_Mp4);
videoWidget->getVideoThread()->setSaveAudioType(SaveAudioType_None);
//从录像计划表中找出对应的计划
int index = videoWidgets.indexOf(videoWidget);
QStringList recordTime = recordTimes.at(index);
QString time = recordTime.at(recordWeek);
QStringList list = time.split(",");
bool needRecord = (list.at(recordIndex) == "1");
if (needRecord) {
QString path = recordPath + "/" + QDATE;
QString fileName = QString("%1/ch%2_%3.mp4").arg(path).arg(index + 1, 2, 10, QChar('0')).arg(STRDATETIMEMS);
videoWidget->recordStart(fileName);
}
}
void VideoManage::receivePlayStart(int time)
{
lastRecordTime = QDateTime::currentDateTime();
VideoWidget *videoWidget = (VideoWidget *)sender();
this->doRecord(videoWidget, lastRecordTime);
}
void VideoManage::checkRecord()
{
//如果还在打开阶段则不用继续
if (openIndex < videoWidgets.count()) {
return;
}
//没有开启录像存储则不处理
if (!saveVideo) {
msleep(1000);
return;
}
//计算离最后一次开启录像的时间差值(防止频繁执行)
QDateTime now = QDateTime::currentDateTime();
int offset = lastRecordTime.secsTo(now);
if (offset > 60) {
//无论是否需要录像到整半点就处理一下
int min = now.time().minute();
int sec = now.time().second();
//这里设定一个目标秒数可以用于校准时间精确到秒(偏差几秒就填几/默认无偏差就是0)
int target = 0;
if ((min == 0 || min == 30) && sec >= target) {
lastRecordTime = now;
//先对所有的通道停止录像/再去判断是否需要录像再开启录像
foreach (VideoWidget *videoWidget, videoWidgets) {
videoWidget->recordStop();
this->doRecord(videoWidget, now);
}
}
this->initPath();
}
msleep(1000);
}
void VideoManage::setMediaUrls(const QStringList &mediaUrls)
{
this->mediaUrls = mediaUrls;
}
void VideoManage::setVideoWidgets(QList<VideoWidget *> videoWidgets)
{
//关联打开成功信号开启录像/末尾参数用来过滤只关联一次
this->videoWidgets = videoWidgets;
foreach (VideoWidget *videoWidget, videoWidgets) {
connect(videoWidget, SIGNAL(sig_receivePlayStart(int)), this, SLOT(receivePlayStart(int)), Qt::UniqueConnection);
}
}
void VideoManage::setRecordTimes(const QList<QStringList> &recordTimes)
{
this->recordTimes = recordTimes;
}
void VideoManage::setOpenInterval(int openInterval)
{
if (openInterval >= 0 && openInterval < 3000) {
this->openInterval = openInterval;
}
}
void VideoManage::setSaveVideo(bool saveVideo)
{
this->saveVideo = saveVideo;
}
void VideoManage::setPath(const QString &recordPath, const QString &snapPath)
{
this->recordPath = recordPath;
this->snapPath = snapPath;
this->initPath();
}
void VideoManage::stop()
{
//处于运行状态才可以停止
if (this->isRunning()) {
stopped = true;
this->wait();
}
//关闭所有视频
foreach (VideoWidget *videoWidget, videoWidgets) {
videoWidget->stop();
}
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。