JustMotion/src/main.cpp

240 lines
7.2 KiB
C++
Raw Normal View History

// This file is part of Motion Watch.
2022-04-14 09:45:54 -04:00
// 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.
2022-04-14 09:45:54 -04:00
// 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.
2022-04-14 09:45:54 -04:00
#include "mo_detect.h"
#include "logger.h"
void detectMoInFile(const string &detPath, const string &recPath, shared_t *share)
2022-04-14 09:45:54 -04:00
{
if (fork() == 0)
{
detLog("detect_mo_in_file() -- start", share);
share->detProcs++;
if (exists(detPath))
{
Mat thumbNail;
if (moDetect(detPath, thumbNail, share))
{
share->skipCmd = true;
wrOut(recPath, thumbNail, share);
}
}
if (exists(detPath)) remove(detPath);
if (exists(recPath)) remove(recPath);
share->detProcs--;
detLog("detect_mo_in_file() -- finished", share);
}
}
void exeCmd(char *const argv[])
{
if (fork() == 0)
{
execvp("ffmpeg", argv);
_Exit(EXIT_FAILURE);
}
}
void recLoop(shared_t *share)
{
while (rdConf(share))
{
enforceMaxLogSize(share->recLogPath, share);
enforceMaxLogSize(share->detLogPath, share);
initLogFile(share->recLogPath, share->recLogFile);
initLogFile(share->detLogPath, share->detLogFile);
initLogFrontPages(share);
recLog("rec_loop() -- start", share);
if (!exists("/tmp/mow-lock") && share->updateRoot)
{
system("touch /tmp/mow-lock");
genCSS(share);
genHTMLul(share->webRoot, string(APP_NAME) + " " + string(APP_VER), share);
remove("/tmp/mow-lock");
recLog("webroot page updated: " + cleanDir(share->webRoot) + "/index.html", share);
share->updateRoot = false;
}
genHTMLul(share->outDir, share->camName, share);
recLog("camera specific webroot page updated: " + share->outDir + "/index.html", share);
auto strClipLen = to_string(share->clipLen);
char* argvRec[] = {(char*) "ffmpeg",
(char*) "-hide_banner",
(char*) "-i",
(char*) share->recordUrl.c_str(),
(char*) "-y",
(char*) "-vcodec",
(char*) share->vidCodec.c_str(),
(char*) "-t",
(char*) strClipLen.c_str(),
(char*) "--replace_me--",
NULL};
char* argvDet[] = {(char*) "ffmpeg",
(char*) "-hide_banner",
(char*) "-i",
(char*) share->detectUrl.c_str(),
(char*) "-y",
(char*) "-vcodec",
(char*) share->vidCodec.c_str(),
(char*) "-t",
(char*) strClipLen.c_str(),
(char*) "--replace_me--",
NULL};
for (auto i = 0; i < share->numOfClips; ++i, ++share->index)
{
auto detPath = cleanDir(share->buffDir) + "/" + to_string(share->index) + share->detSuffix;
auto recPath = cleanDir(share->buffDir) + "/" + to_string(share->index) + share->recSuffix;
if (share->recordUrl == share->detectUrl)
{
recPath = detPath;
}
argvRec[PATH_ADDR] = (char*) recPath.c_str();
argvDet[PATH_ADDR] = (char*) detPath.c_str();
recLog("fetching camera footage -- ", share);
if (share->recordUrl == share->detectUrl)
{
exeCmd(argvDet);
}
else
{
exeCmd(argvDet);
exeCmd(argvRec);
}
sleep(share->clipLen * 2);
detectMoInFile(detPath, recPath, share);
}
while (share->detProcs > 0) sleep(1);
if (!share->skipCmd)
{
recLog("no motion detected", share);
if (share->postCmd.empty())
{
recLog("post command not defined, skipping.", share);
}
else
{
recLog("running post command: " + share->postCmd, share);
system(string("timeout -k 1 14 " + share->postCmd).c_str());
}
}
else
{
recLog("motion detected, skipping the post command.", share);
}
recLog("rec_loop() -- finished", share);
if (share->retCode != 0)
{
break;
}
destroy_at(argvDet);
destroy_at(argvRec);
}
}
void sigHandler(int signal, siginfo_t *info, void *context)
{
cerr << "received signal details: " << endl;
cerr << "-- si_pid: " << info->si_pid << endl;
cerr << "-- si_signo: " << info->si_signo << endl;
cerr << "-- si_code: " << info->si_code << endl;
cerr << "-- si_errno: " << info->si_errno << endl;
cerr << "-- si_uid: " << info->si_uid << endl;
cerr << "-- si_addr: " << info->si_addr << endl;
cerr << "-- si_status: " << info->si_status << endl;
cerr << "-- si_band: " << info->si_band << endl;
exit(EXIT_FAILURE);
}
int main(int argc, char** argv)
{
struct shared_t sharedRes;
struct sigaction act = { 0 };
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = &sigHandler;
sigaction(SIGTERM, &act, NULL);
sigaction(SIGABRT, &act, NULL);
sigaction(SIGSEGV, &act, NULL);
sigaction(SIGILL, &act, NULL);
sigaction(SIGFPE, &act, NULL);
sigaction(SIGBUS, &act, NULL);
sharedRes.conf = parseForParam("-c", argc, argv, false);
if (parseForParam("-h", argc, argv, true) == "true")
2022-04-14 09:45:54 -04:00
{
cout << "Motion Watch " << APP_VER << endl << endl;
cout << "Usage: mow <argument>" << endl << endl;
cout << "-h : display usage information about this application." << endl;
cout << "-c : path to the config file." << endl;
cout << "-v : display the current version." << endl << endl;
}
else if (parseForParam("-v", argc, argv, true) == "true")
{
cout << APP_VER << endl;
2022-04-14 09:45:54 -04:00
}
else if (sharedRes.conf.empty())
2022-04-14 09:45:54 -04:00
{
cerr << "err: config file not given in -c" << endl;
2022-04-14 09:45:54 -04:00
}
else
{
sharedRes.retCode = 0;
sharedRes.detProcs = 0;
sharedRes.index = 0;
sharedRes.updateRoot = true;
sharedRes.skipCmd = false;
sharedRes.init = true;
recLoop(&sharedRes);
2022-04-14 09:45:54 -04:00
return sharedRes.retCode;
2022-04-14 09:45:54 -04:00
}
return EINVAL;
2022-04-14 09:45:54 -04:00
}