daad0dffa2
-re-added recordloop as a thread within the app. -the app no longer use mutiple services and will instead fully operate in a single master service. -build/install.py will now install the app as a single service. -added/updated -s, -r and -q options to manage the single master service.
386 lines
9.7 KiB
C++
386 lines
9.7 KiB
C++
// This file is part of JustMotion.
|
|
|
|
// JustMotion 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.
|
|
|
|
// JustMotion 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 "common.h"
|
|
|
|
QStringList lsFilesInDir(const QString &path, const QString &ext)
|
|
{
|
|
QStringList filters;
|
|
|
|
filters << "*" + ext;
|
|
|
|
QDir dirObj(path);
|
|
|
|
dirObj.setFilter(QDir::Files);
|
|
dirObj.setNameFilters(filters);
|
|
dirObj.setSorting(QDir::Name);
|
|
|
|
return dirObj.entryList();
|
|
}
|
|
|
|
QStringList lsDirsInDir(const QString &path)
|
|
{
|
|
QDir dirObj(path);
|
|
|
|
dirObj.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
|
|
dirObj.setSorting(QDir::Name);
|
|
|
|
return dirObj.entryList();
|
|
}
|
|
|
|
QStringList listFacingFiles(const QString &path, const QString &ext, const QDateTime &stamp, int secs, char dir)
|
|
{
|
|
QStringList ret;
|
|
|
|
for (auto i = 0; i < secs; ++i)
|
|
{
|
|
QString filePath;
|
|
|
|
if (dir == '-') filePath = path + "/" + stamp.addSecs(-i).toString(DATETIME_FMT) + ext;
|
|
if (dir == '+') filePath = path + "/" + stamp.addSecs(i).toString(DATETIME_FMT) + ext;
|
|
|
|
if (QFile::exists(filePath))
|
|
{
|
|
if (dir == '-') ret.insert(0, filePath);
|
|
if (dir == '+') ret.append(filePath);
|
|
}
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
QStringList backwardFacingFiles(const QString &path, const QString &ext, const QDateTime &stamp, int secs)
|
|
{
|
|
return listFacingFiles(path, ext, stamp, secs, '-');
|
|
}
|
|
|
|
QStringList forwardFacingFiles(const QString &path, const QString &ext, const QDateTime &stamp, int secs)
|
|
{
|
|
return listFacingFiles(path, ext, stamp, secs, '+');
|
|
}
|
|
|
|
void enforceMaxEvents(shared_t *share)
|
|
{
|
|
auto names = lsFilesInDir(share->recPath, share->recExt);
|
|
|
|
while (names.size() > share->maxEvents)
|
|
{
|
|
auto nameOnly = share->recPath + "/" + names[0];
|
|
|
|
nameOnly.chop(share->recExt.size());
|
|
|
|
auto vidFile = nameOnly + share->recExt;
|
|
auto imgFile = nameOnly + share->thumbExt;
|
|
|
|
QFile::remove(vidFile);
|
|
QFile::remove(imgFile);
|
|
|
|
names.removeFirst();
|
|
}
|
|
}
|
|
|
|
void enforceMaxImages(shared_t *share)
|
|
{
|
|
auto names = lsFilesInDir(share->buffPath + "/img", ".bmp");
|
|
|
|
while (names.size() > (share->liveSecs / 2))
|
|
{
|
|
QFile::remove(share->buffPath + "/img/" + names[0]);
|
|
|
|
names.removeFirst();
|
|
}
|
|
}
|
|
|
|
void enforceMaxClips(shared_t *share)
|
|
{
|
|
auto names = lsFilesInDir(share->buffPath + "/live", share->streamExt);
|
|
|
|
while (names.size() > (share->liveSecs / 2))
|
|
{
|
|
QFile::remove(share->buffPath + "/live/" + names[0]);
|
|
|
|
names.removeFirst();
|
|
}
|
|
}
|
|
|
|
void rdLine(const QString ¶m, const QString &line, QString *value)
|
|
{
|
|
if (line.startsWith(param))
|
|
{
|
|
*value = line.mid(param.size()).trimmed();
|
|
}
|
|
}
|
|
|
|
void rdLine(const QString ¶m, const QString &line, int *value)
|
|
{
|
|
if (line.startsWith(param))
|
|
{
|
|
*value = line.mid(param.size()).trimmed().toInt();
|
|
}
|
|
}
|
|
|
|
void rdLine(const QString ¶m, const QString &line, bool *value)
|
|
{
|
|
if (line.startsWith(param))
|
|
{
|
|
auto val = line.mid(param.size()).trimmed();
|
|
|
|
*value = (val == "y" || val == "Y");
|
|
}
|
|
}
|
|
|
|
void extCorrection(QString &ext)
|
|
{
|
|
if (!ext.startsWith("."))
|
|
{
|
|
ext = "." + ext;
|
|
}
|
|
}
|
|
|
|
bool mkPath(const QString &path)
|
|
{
|
|
auto ret = true;
|
|
|
|
if (!QDir().exists(path))
|
|
{
|
|
ret = QDir().mkpath(path);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
bool rdConf(const QString &filePath, shared_t *share)
|
|
{
|
|
QFile varFile(filePath);
|
|
|
|
if (!varFile.open(QFile::ReadOnly))
|
|
{
|
|
share->retCode = ENOENT;
|
|
|
|
QTextStream(stderr) << "err: config file - " << filePath << " does not exists or lack read permissions." << Qt::endl;
|
|
}
|
|
else
|
|
{
|
|
share->recordUri.clear();
|
|
share->postCmd.clear();
|
|
share->camName.clear();
|
|
share->buffPath.clear();
|
|
share->recPath.clear();
|
|
|
|
share->retCode = 0;
|
|
share->imgThresh = 8000;
|
|
share->maxEvents = 30;
|
|
share->skipCmd = false;
|
|
share->postSecs = 60;
|
|
share->evMaxSecs = 30;
|
|
share->conf = filePath;
|
|
share->outputType = "stderr";
|
|
share->compCmd = "magick compare -metric FUZZ " + QString(PREV_IMG) + " " + QString(NEXT_IMG) + " /dev/null";
|
|
share->vidCodec = "copy";
|
|
share->audCodec = "copy";
|
|
share->streamExt = ".mkv";
|
|
share->recExt = ".mkv";
|
|
share->thumbExt = ".jpg";
|
|
share->recFps = 30;
|
|
share->liveSecs = 80;
|
|
share->recScale = "1280:720";
|
|
share->imgScale = "320:240";
|
|
|
|
QString line;
|
|
|
|
do
|
|
{
|
|
line = QString::fromUtf8(varFile.readLine());
|
|
|
|
if (!line.startsWith("#"))
|
|
{
|
|
rdLine("cam_name = ", line, &share->camName);
|
|
rdLine("recording_uri = ", line, &share->recordUri);
|
|
rdLine("buffer_path = ", line, &share->buffPath);
|
|
rdLine("rec_path = ", line, &share->recPath);
|
|
rdLine("max_event_secs = ", line, &share->evMaxSecs);
|
|
rdLine("post_secs = ", line, &share->postSecs);
|
|
rdLine("post_cmd = ", line, &share->postCmd);
|
|
rdLine("img_thresh = ", line, &share->imgThresh);
|
|
rdLine("max_events = ", line, &share->maxEvents);
|
|
rdLine("img_comp_out = ", line, &share->outputType);
|
|
rdLine("img_comp_cmd = ", line, &share->compCmd);
|
|
rdLine("vid_codec = ", line, &share->vidCodec);
|
|
rdLine("aud_codec = ", line, &share->audCodec);
|
|
rdLine("stream_ext = ", line, &share->streamExt);
|
|
rdLine("rec_ext = ", line, &share->recExt);
|
|
rdLine("thumbnail_ext = ", line, &share->thumbExt);
|
|
rdLine("rec_fps = ", line, &share->recFps);
|
|
rdLine("rec_scale = ", line, &share->recScale);
|
|
rdLine("img_scale = ", line, &share->imgScale);
|
|
rdLine("live_secs = ", line, &share->liveSecs);
|
|
}
|
|
|
|
} while(!line.isEmpty());
|
|
|
|
if (share->camName.isEmpty())
|
|
{
|
|
share->camName = QFileInfo(share->conf).baseName();
|
|
}
|
|
|
|
extCorrection(share->streamExt);
|
|
extCorrection(share->recExt);
|
|
extCorrection(share->thumbExt);
|
|
|
|
if (share->outputType != "stdout" && share->outputType != "stderr")
|
|
{
|
|
share->outputType = "stderr";
|
|
}
|
|
|
|
if (share->buffPath.isEmpty())
|
|
{
|
|
share->buffPath = "/var/buffer/" + share->camName;
|
|
}
|
|
else
|
|
{
|
|
share->buffPath = QDir::cleanPath(share->buffPath);
|
|
}
|
|
|
|
if (share->recPath.isEmpty())
|
|
{
|
|
share->recPath = "/var/footage/" + share->camName;
|
|
}
|
|
else
|
|
{
|
|
share->recPath = QDir::cleanPath(share->recPath);
|
|
}
|
|
|
|
if (share->liveSecs < 10)
|
|
{
|
|
share->liveSecs = 10;
|
|
}
|
|
|
|
if ((share->liveSecs - 4) < share->evMaxSecs)
|
|
{
|
|
share->evMaxSecs = share->liveSecs - 4;
|
|
}
|
|
}
|
|
|
|
return share->retCode == 0;
|
|
}
|
|
|
|
void setupBuffDir(const QString &path, bool del)
|
|
{
|
|
if (del)
|
|
{
|
|
QDir(path).removeRecursively();
|
|
}
|
|
|
|
if (!QFileInfo::exists(path))
|
|
{
|
|
mkPath(path);
|
|
}
|
|
|
|
if (!QFileInfo::exists(path + "/live"))
|
|
{
|
|
mkPath(path + "/live");
|
|
}
|
|
|
|
if (!QFileInfo::exists(path + "/img"))
|
|
{
|
|
mkPath(path + "/img");
|
|
}
|
|
}
|
|
|
|
QString buildThreadCount(int count)
|
|
{
|
|
QString ret = "0";
|
|
|
|
for (auto i = 1; i < count; ++i)
|
|
{
|
|
ret.append(","); ret.append(QString::number(i));
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
QStringList parseArgs(const QByteArray &data, int maxArgs, int *pos)
|
|
{
|
|
QStringList ret;
|
|
QString arg;
|
|
|
|
auto line = QString::fromUtf8(data);
|
|
auto inDQuotes = false;
|
|
auto inSQuotes = false;
|
|
auto escaped = false;
|
|
|
|
if (pos != nullptr) *pos = 0;
|
|
|
|
for (int i = 0; i < line.size(); ++i)
|
|
{
|
|
if (pos != nullptr) *pos += 1;
|
|
|
|
if ((line[i] == '\'') && !inDQuotes && !escaped)
|
|
{
|
|
// single quote '
|
|
|
|
inSQuotes = !inSQuotes;
|
|
}
|
|
else if ((line[i] == '\"') && !inSQuotes && !escaped)
|
|
{
|
|
// double quote "
|
|
|
|
inDQuotes = !inDQuotes;
|
|
}
|
|
else
|
|
{
|
|
escaped = false;
|
|
|
|
if (line[i].isSpace() && !inDQuotes && !inSQuotes)
|
|
{
|
|
// space
|
|
|
|
if (!arg.isEmpty())
|
|
{
|
|
ret.append(arg);
|
|
arg.clear();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if ((line[i] == '\\') && ((i + 1) < line.size()))
|
|
{
|
|
if ((line[i + 1] == '\'') || (line[i + 1] == '\"'))
|
|
{
|
|
escaped = true;
|
|
}
|
|
else
|
|
{
|
|
arg.append(line[i]);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
arg.append(line[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
if ((ret.size() >= maxArgs) && (maxArgs != -1))
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!arg.isEmpty() && !inDQuotes && !inSQuotes)
|
|
{
|
|
ret.append(arg);
|
|
}
|
|
|
|
return ret;
|
|
}
|