From f850ec6a46751732b3bd8988ad302b6a3ae9575d Mon Sep 17 00:00:00 2001 From: Maurice ONeal Date: Sun, 21 May 2023 09:34:57 -0400 Subject: [PATCH] v3.0.t5 -fixed all loop structors used throughout the app. they were running too fast. -added more log lines to aid with debug. --- install.sh | 1 + setup.sh | 2 +- src/camera.cpp | 154 +++++++++++++++++++++++----------------------- src/camera.h | 5 +- src/common.h | 3 +- src/main.cpp | 2 - src/mo_detect.cpp | 4 +- 7 files changed, 84 insertions(+), 87 deletions(-) diff --git a/install.sh b/install.sh index ac8de0e..f83e5aa 100644 --- a/install.sh +++ b/install.sh @@ -1,4 +1,5 @@ #!/bin/sh +apt install apache2 if [ ! -d "/opt/mow" ]; then mkdir /opt/mow fi diff --git a/setup.sh b/setup.sh index fb6c338..90cad0a 100644 --- a/setup.sh +++ b/setup.sh @@ -1,4 +1,4 @@ #!/bin/sh apt update -y apt install -y pkg-config cmake make g++ wget unzip git -apt install -y ffmpeg libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libgstreamer1.0-dev x264 libx264-dev libilmbase-dev libopencv-dev qt6-base-dev qtchooser qmake6 qt6-base-dev-tools qt6-multimedia-dev apache2 +apt install -y ffmpeg libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libgstreamer1.0-dev x264 libx264-dev libilmbase-dev libopencv-dev qt6-base-dev qtchooser qmake6 qt6-base-dev-tools qt6-multimedia-dev libxkbcommon-dev diff --git a/src/camera.cpp b/src/camera.cpp index e6df08b..ba2f4ee 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -36,18 +36,16 @@ Camera::Camera(QObject *parent) : QObject(parent) int Camera::start(const QStringList &args) { - auto ret = false; - shared.conf = getParam("-c", args); if (rdConf(&shared)) { QDir("live").removeRecursively(); - auto thr1 = new QThread(QCoreApplication::instance()); - auto thr2 = new QThread(QCoreApplication::instance()); - auto thr3 = new QThread(QCoreApplication::instance()); - auto thr4 = new QThread(QCoreApplication::instance()); + auto thr1 = new QThread(nullptr); + auto thr2 = new QThread(nullptr); + auto thr3 = new QThread(nullptr); + auto thr4 = new QThread(nullptr); new RecLoop(&shared, thr1, this); new Upkeep(&shared, thr2, this); @@ -67,32 +65,29 @@ Loop::Loop(shared_t *sharedRes, QThread *thr, QObject *parent) : QObject(0) { shared = sharedRes; heartBeat = 10; + loopTimer = new QTimer(nullptr); - connect(this, &Loop::loopSig, this, &Loop::loopSlot); - connect(thr, &QThread::started, this, &Loop::init); - connect(parent, &QObject::destroyed, this, &Loop::deleteLater); - connect(parent, &QObject::destroyed, thr, &QThread::terminate); + loopTimer->setSingleShot(false); + + connect(thr, &QThread::started, this, &Loop::init); + connect(parent, &QObject::destroyed, this, &Loop::deleteLater); + connect(parent, &QObject::destroyed, thr, &QThread::terminate); + connect(parent, &QObject::destroyed, loopTimer, &QTimer::deleteLater); + connect(loopTimer, &QTimer::timeout, this, &Loop::loopSlot); moveToThread(thr); } void Loop::init() { - emit loopSig(); + loopTimer->start(heartBeat * 1000); } void Loop::loopSlot() { - auto ret = exec(); - - if (heartBeat != 0) + if (!exec()) { - thread()->sleep(heartBeat); - } - - if (ret) - { - emit loopSig(); + loopTimer->stop(); QCoreApplication::exit(shared->retCode); } } @@ -156,6 +151,8 @@ bool RecLoop::exec() auto hashLogLine = "stream_hash--prev:" + QString(streamMD5.toHex()) + "--new:" + QString(md5.toHex()); + streamMD5 = md5; + recLog(hashLogLine, shared); if (proc.state() == QProcess::NotRunning) @@ -316,7 +313,7 @@ bool EventLoop::wrOutVod(const evt_t &event) DetectLoop::DetectLoop(shared_t *sharedRes, QThread *thr, QObject *parent) : Loop(sharedRes, thr, parent) { - heartBeat = 0; + heartBeat = 1; evId = 0; pcId = 0; player = new QMediaPlayer(this); @@ -332,6 +329,8 @@ void DetectLoop::init() thread()->sleep(1); resetTimers(); + + Loop::init(); } void DetectLoop::resetTimers() @@ -339,8 +338,8 @@ void DetectLoop::resetTimers() if (evId != 0) killTimer(evId); if (pcId != 0) killTimer(pcId); - evId = startTimer(shared->evMaxSecs); - pcId = startTimer(shared->postSecs); + evId = startTimer(shared->evMaxSecs * 1000); + pcId = startTimer(shared->postSecs * 1000); } void DetectLoop::timerEvent(QTimerEvent *event) @@ -389,83 +388,82 @@ void DetectLoop::timerEvent(QTimerEvent *event) } } -void DetectLoop::resetDetect() -{ - if (player->playbackState() != QMediaPlayer::PlayingState) - { - frameAnalyzer->stop(); - - exec(); - } -} - void DetectLoop::playError(QMediaPlayer::Error error, const QString &errorString) { Q_UNUSED(error) detLog("err: media player error - " + errorString, shared); - resetDetect(); } void DetectLoop::playStateChanged(QMediaPlayer::PlaybackState newState) { - Q_UNUSED(newState) - - resetDetect(); + if (newState == QMediaPlayer::PlayingState) + { + detLog("detection playback started.", shared); + } + else if (newState == QMediaPlayer::StoppedState) + { + detLog("detection playback stopped.", shared); + } } bool DetectLoop::exec() { - QFile fileIn("stream.m3u8"); - QString tsPath; + if (player->playbackState() != QMediaPlayer::PlayingState) + { + QFile fileIn("stream.m3u8"); + QString tsPath; - if (!fileIn.open(QFile::ReadOnly)) - { - detLog("err: failed to open the stream hls file for reading. reason: " + fileIn.errorString(), shared); - } - else if (fileIn.size() < 50) - { - detLog("the stream hls list is not big enough yet. waiting for more clips.", shared); - } - else if (!fileIn.seek(fileIn.size() - 50)) - { - detLog("err: failed to seek to 'near end' of stream file. reason: " + fileIn.errorString(), shared); - } - else - { - QString line; - - do + if (!fileIn.open(QFile::ReadOnly)) { - line = QString::fromUtf8(fileIn.readLine()); + detLog("err: failed to open the stream hls file for reading. reason: " + fileIn.errorString(), shared); + } + else if (fileIn.size() < 50) + { + detLog("the stream hls list is not big enough yet. waiting for more clips.", shared); + } + else if (!fileIn.seek(fileIn.size() - 50)) + { + detLog("err: failed to seek to 'near end' of stream file. reason: " + fileIn.errorString(), shared); + } + else + { + QString line; - if (line.startsWith("live/")) + do { - tsPath = line; - } + line = QString::fromUtf8(fileIn.readLine()); - } while(!line.isEmpty()); - } + if (line.startsWith("live/")) + { + tsPath = line; + } - if (tsPath.isEmpty()) - { - detLog("wrn: didn't find the latest hls clip. previous failure? waiting 5secs.", shared); + } while(!line.isEmpty()); + } - thread()->sleep(5); - } - else if (prevTs == tsPath) - { - detLog("wrn: the lastest hls clip is the same as the previous clip. is the recording loop running? waiting 5secs.", shared); + if (tsPath.isEmpty()) + { + detLog("wrn: didn't find the latest hls clip. previous failure? waiting 5secs.", shared); - thread()->sleep(5); - } - else - { - prevTs = tsPath; + thread()->sleep(5); + } + else if (prevTs == tsPath) + { + detLog("wrn: the lastest hls clip is the same as the previous clip. is the recording loop running? waiting 5secs.", shared); - player->setSource(QUrl::fromLocalFile(tsPath)); - frameAnalyzer->play(tsPath); - player->play(); + thread()->sleep(5); + } + else + { + detLog("stream_clip: " + tsPath, shared); + + prevTs = tsPath; + + player->setSource(QUrl::fromLocalFile(tsPath)); + frameAnalyzer->play(tsPath); + player->play(); + } } return Loop::exec(); diff --git a/src/camera.h b/src/camera.h index 642ef8a..8e24d51 100644 --- a/src/camera.h +++ b/src/camera.h @@ -40,6 +40,7 @@ class Loop : public QObject protected: shared_t *shared; + QTimer *loopTimer; int heartBeat; protected slots: @@ -50,10 +51,6 @@ private slots: void loopSlot(); -signals: - - void loopSig(); - public: explicit Loop(shared_t *shared, QThread *thr, QObject *parent = nullptr); diff --git a/src/common.h b/src/common.h index d65459f..39cb57f 100644 --- a/src/common.h +++ b/src/common.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -35,7 +36,7 @@ using namespace cv; using namespace std; -#define APP_VER "3.0.t4" +#define APP_VER "3.0.t5" #define APP_NAME "Motion Watch" #define APP_BIN "mow" #define REC_LOG_NAME "rec_log_lines.html" diff --git a/src/main.cpp b/src/main.cpp index 81b1ba4..78779ee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,8 +20,6 @@ int main(int argc, char** argv) QCoreApplication::setApplicationName(APP_NAME); QCoreApplication::setApplicationVersion(APP_VER); - cv::utils::logging::setLogLevel(cv::utils::logging::LogLevel::LOG_LEVEL_DEBUG); - auto args = QCoreApplication::arguments(); auto ret = 0; diff --git a/src/mo_detect.cpp b/src/mo_detect.cpp index 15339f4..2eec30f 100644 --- a/src/mo_detect.cpp +++ b/src/mo_detect.cpp @@ -41,9 +41,11 @@ void MoDetect::newFrame() } else { + gap = 0; + auto nextImg = videoFrame().toImage(); auto nextImgGray = nextImg.convertToFormat(QImage::Format_Grayscale8); - auto score = 0; + auto score = 0; if (diff(baseImg, nextImgGray, &score)) {