// This file is part of Motion Watch. // Motion Watch is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Motion Watch is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. #include "camera.h" Camera::Camera(QObject *parent) : QObject(parent) {} int Camera::start(const QStringList &args) { if (rdConf(getParam("-c", args), &shared)) { auto thr1 = new QThread(nullptr); auto thr2 = new QThread(nullptr); auto thr3 = new QThread(nullptr); new Upkeep(&shared, thr1, nullptr); new EventLoop(&shared, thr2, nullptr); new DetectLoop(&shared, thr3, nullptr); thr1->start(); thr2->start(); thr3->start(); } return shared.retCode; } Loop::Loop(shared_t *sharedRes, QThread *thr, QObject *parent) : QObject(parent) { shared = sharedRes; heartBeat = 10; loopTimer = 0; connect(thr, &QThread::started, this, &Loop::init); moveToThread(thr); } void Loop::init() { loopTimer = new QTimer(this); connect(loopTimer, &QTimer::timeout, this, &Loop::loopSlot); loopTimer->setSingleShot(false); loopTimer->start(heartBeat * 1000); loopSlot(); } void Loop::loopSlot() { if (!exec()) { loopTimer->stop(); QCoreApplication::exit(shared->retCode); } } bool Loop::exec() { if (loopTimer->interval() != heartBeat * 1000) { loopTimer->start(heartBeat * 1000); } return shared->retCode == 0; } Upkeep::Upkeep(shared_t *sharedRes, QThread *thr, QObject *parent) : Loop(sharedRes, thr, parent) {} bool Upkeep::exec() { enforceMaxEvents(shared); enforceMaxImages(shared); enforceMaxVids(shared); return Loop::exec(); } EventLoop::EventLoop(shared_t *sharedRes, QThread *thr, QObject *parent) : Loop(sharedRes, thr, parent) { heartBeat = 2; highScore = 0; cycles = 0; } bool EventLoop::exec() { if (!vidList.isEmpty()) { vidList.removeDuplicates(); if (vidList.size() > 1) { QTextStream(stdout) << "attempting write out of event: " << name << Qt::endl; if (wrOutVod()) { QProcess proc; QStringList args; args << "convert"; args << imgPath; args << shared->recPath + "/" + name + shared->thumbExt; proc.start("magick", args); proc.waitForFinished(); } } cycles = 0; highScore = 0; vidList.clear(); } QList rmIndx; for (auto i = 0; i < shared->recList.size(); ++i) { auto event = &shared->recList[i]; if (highScore < event->score) { name = event->timeStamp.toString(DATETIME_FMT); imgPath = event->imgPath; highScore = event->score; } if (event->queAge >= (shared->evMaxSecs / heartBeat)) { auto maxSecs = shared->evMaxSecs / 2; // half the maxsecs value to get front-back half secs auto backFiles = backwardFacingFiles(shared->buffPath + "/live", shared->streamExt, event->timeStamp, maxSecs); auto frontFiles = forwardFacingFiles(shared->buffPath + "/live", shared->streamExt, event->timeStamp, maxSecs); vidList.append(backFiles + frontFiles); rmIndx.append(i); } else { event->queAge += heartBeat; } } for (auto i : rmIndx) { shared->recList.removeAt(i); } return Loop::exec(); } bool EventLoop::wrOutVod() { auto cnt = 0; auto concat = name + ".tmp"; auto ret = false; QFile file(concat); file.open(QFile::WriteOnly); for (auto &&vid : vidList) { QTextStream(stdout) << "event_src: " << vid << Qt::endl; if (QFile::exists(vid)) { file.write(QString("file '" + vid + "'\n").toUtf8()); cnt++; } else { QTextStream(stdout) << "warning: the event hls clip does not exists." << Qt::endl; } } file.close(); if (cnt == 0) { QTextStream(stderr) << "err: none of the event hls clips exists, canceling write out." << Qt::endl; QFile::remove(concat); } else { QStringList args; args << "-f"; args << "concat"; args << "-safe" << "0"; args << "-i" << concat; args << "-c" << "copy"; args << shared->recPath + "/" + name + shared->recExt; QProcess::execute("ffmpeg", args); QFile::remove(concat); } return ret; } DetectLoop::DetectLoop(shared_t *sharedRes, QThread *thr, QObject *parent) : Loop(sharedRes, thr, parent) { seed = 0; pcTimer = 0; heartBeat = 2; delayCycles = 12; // this will be used to delay the // actual start of DetectLoop by // 24secs. } void DetectLoop::init() { pcTimer = new QTimer(this); mod = false; connect(pcTimer, &QTimer::timeout, this, &DetectLoop::pcBreak); resetTimers(); Loop::init(); } void DetectLoop::resetTimers() { pcTimer->start(shared->postSecs * 1000); } void DetectLoop::pcBreak() { if (!shared->postCmd.isEmpty()) { QTextStream(stdout) << "---POST_BREAK---" << Qt::endl; if (mod) { QTextStream(stdout) << "motion detected, skipping the post command." << Qt::endl; } else { if (delayCycles == 0) delayCycles = 5; else delayCycles += 5; QTextStream(stdout) << "no motion detected, running post command: " << shared->postCmd << Qt::endl; auto args = parseArgs(shared->postCmd.toUtf8(), -1); if (args.isEmpty()) { QTextStream(stderr) << "err: did not parse an executable from the post command line." << Qt::endl; } else { QProcess::execute(args[0], args.mid(1)); } } } mod = false; } float DetectLoop::getFloatFromExe(const QByteArray &line) { QString strLine(line); QString strNum; for (auto chr : strLine) { if (chr.isDigit() || (chr == '.')) { strNum.append(chr); } else { break; } } return strNum.toFloat(); } QStringList DetectLoop::buildArgs(const QString &prev, const QString &next) { auto args = parseArgs(shared->compCmd.toUtf8(), -1); for (auto i = 0; i < args.size(); ++i) { if (args[i] == PREV_IMG) args[i] = prev; if (args[i] == NEXT_IMG) args[i] = next; } return args; } QStringList DetectLoop::buildSnapArgs(const QString &vidSrc, const QString &imgPath) { QStringList ret; ret.append("-y"); ret.append("-i"); ret.append(vidSrc); ret.append("-frames:v"); ret.append("1"); ret.append(imgPath); return ret; } bool DetectLoop::exec() { if (delayCycles > 0) { delayCycles -= 1; QTextStream(stdout) << "delay: detection cycle skipped. cycles left to be skipped: " << QString::number(delayCycles) << Qt::endl; } else { auto imgAPath = "img/" + QString::number(seed++) + ".bmp"; auto imgBPath = "img/" + QString::number(seed++) + ".bmp"; auto curDT = QDateTime::currentDateTime(); auto clips = backwardFacingFiles("live", shared->streamExt, curDT, 16); if (clips.size() < 2) { QTextStream(stdout) << "warning: didn't pick up enough clips files from the video stream. number of files: " << QString::number(clips.size()) << Qt::endl; QTextStream(stdout) << " will try again on the next loop." << Qt::endl; } else { auto snapArgsA = buildSnapArgs(clips[clips.size() - 2], imgAPath); auto snapArgsB = buildSnapArgs(clips[clips.size() - 1], imgAPath); auto compArgs = buildArgs(imgAPath, imgBPath); if (compArgs.isEmpty()) { QTextStream(stderr) << "err: could not parse a executable name from img_comp_cmd: " << shared->compCmd << Qt::endl; } else { QProcess snapFromVidA; QProcess snapFromVidB; QProcess extComp; snapFromVidA.start("ffmpeg", snapArgsA); snapFromVidA.waitForFinished(); snapFromVidB.start("ffmpeg", snapArgsB); snapFromVidB.waitForFinished(); extComp.start(compArgs[0], compArgs.mid(1)); extComp.waitForFinished(); float score = 0; auto ok = true; if (shared->outputType == "stdout") { score = getFloatFromExe(extComp.readAllStandardOutput()); } else if (shared->outputType == "stderr") { score = getFloatFromExe(extComp.readAllStandardError()); } else { ok = false; } if (!ok) { QTextStream(stderr) << "err: img_comp_out: " << shared->outputType << " is not valid. it must be 'stdout' or 'stderr'" << Qt::endl; } else { QTextStream(stdout) << compArgs.join(" ") << " --result: " << QString::number(score) << Qt::endl; if (score >= shared->imgThresh) { QTextStream(stdout) << "--threshold_breached: " << QString::number(shared->imgThresh) << Qt::endl; evt_t event; event.timeStamp = curDT; event.score = score; event.imgPath = imgBPath; event.queAge = 0; mod = true; shared->recList.append(event); } } } } } return Loop::exec(); }