2022-09-22 20:57:46 -04:00
|
|
|
// This file is part of Motion Watch.
|
2022-04-14 09:45:54 -04:00
|
|
|
|
2022-09-22 20:57:46 -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
|
|
|
|
2022-09-22 20:57:46 -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
|
|
|
|
2022-09-22 20:57:46 -04:00
|
|
|
#include "mo_detect.h"
|
2022-12-11 10:25:22 -05:00
|
|
|
#include "logger.h"
|
2022-08-12 21:46:36 -04:00
|
|
|
|
2022-09-22 20:57:46 -04:00
|
|
|
void detectLoop(shared_t *share)
|
2022-04-14 09:45:54 -04:00
|
|
|
{
|
2022-12-11 10:25:22 -05:00
|
|
|
detLog("detectLoop() -- start", share);
|
|
|
|
|
2022-09-22 20:57:46 -04:00
|
|
|
vector<string> bufFiles;
|
2022-12-13 20:27:32 -05:00
|
|
|
auto waitingForFiles = 0;
|
2022-04-14 09:45:54 -04:00
|
|
|
|
2022-12-04 15:13:39 -05:00
|
|
|
do
|
2022-08-12 21:46:36 -04:00
|
|
|
{
|
2022-09-22 20:57:46 -04:00
|
|
|
bufFiles = lsFilesInDir(share->buffDir, "." + share->vidExt);
|
2022-08-12 21:46:36 -04:00
|
|
|
|
2022-12-04 15:13:39 -05:00
|
|
|
// this loop will not process the last buffile while recLoop is still actively
|
|
|
|
// pulling footage from ffmpeg. it is assumed the last file is not finished.
|
|
|
|
// share->recLoopWait is used to detect if the recloop is still pulling. if not
|
|
|
|
// then the last file is finally processed.
|
|
|
|
|
2022-09-22 20:57:46 -04:00
|
|
|
if ((bufFiles.size() >= 2) || (share->recLoopWait && !bufFiles.empty()))
|
2022-09-11 12:56:32 -04:00
|
|
|
{
|
2022-09-24 09:22:45 -04:00
|
|
|
auto fullPath = cleanDir(share->buffDir) + "/" + bufFiles[0];
|
|
|
|
|
2022-12-04 15:13:39 -05:00
|
|
|
Mat thumbNail;
|
2022-10-02 09:01:38 -04:00
|
|
|
|
2022-12-04 15:13:39 -05:00
|
|
|
if (moDetect(fullPath, thumbNail, share))
|
2022-07-08 15:24:45 -04:00
|
|
|
{
|
2022-10-04 18:12:32 -04:00
|
|
|
share->skipCmd = true;
|
|
|
|
|
2022-12-11 10:25:22 -05:00
|
|
|
detLog("motion detected in file: " + fullPath, share);
|
2022-12-04 15:13:39 -05:00
|
|
|
wrOut(fullPath, thumbNail, share);
|
2022-07-08 15:24:45 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-11 10:25:22 -05:00
|
|
|
detLog("no motion detected in file: " + fullPath + " removing it.", share);
|
2022-09-24 09:22:45 -04:00
|
|
|
remove(fullPath.c_str());
|
2022-04-14 09:45:54 -04:00
|
|
|
}
|
|
|
|
}
|
2022-09-22 20:57:46 -04:00
|
|
|
else
|
2022-08-12 21:46:36 -04:00
|
|
|
{
|
2022-12-13 20:27:32 -05:00
|
|
|
if (waitingForFiles >= share->secs)
|
|
|
|
{
|
|
|
|
detLog("timed out waiting for buff files from ffmpeg, did it fail?", share);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(1); waitingForFiles++;
|
2022-08-12 21:46:36 -04:00
|
|
|
}
|
2022-04-14 09:45:54 -04:00
|
|
|
}
|
2022-12-11 12:33:56 -05:00
|
|
|
while (!bufFiles.empty() && !share->recLoopWait);
|
2022-12-11 10:25:22 -05:00
|
|
|
|
|
|
|
detLog("detectLoop() -- finished", share);
|
2022-07-08 15:24:45 -04:00
|
|
|
}
|
|
|
|
|
2022-09-22 20:57:46 -04:00
|
|
|
void recLoop(shared_t *share)
|
2022-08-12 21:46:36 -04:00
|
|
|
{
|
2022-09-22 20:57:46 -04:00
|
|
|
while (rdConf(share))
|
2022-08-12 21:46:36 -04:00
|
|
|
{
|
2022-12-13 20:27:32 -05:00
|
|
|
enforceMaxLogSize(share->recLogPath, share);
|
|
|
|
enforceMaxLogSize(share->detLogPath, share);
|
|
|
|
|
|
|
|
initLogFile(share->recLogPath, share->recLogFile);
|
|
|
|
initLogFile(share->detLogPath, share->detLogFile);
|
|
|
|
|
|
|
|
initLogFrontPages(share);
|
|
|
|
|
2022-12-11 10:25:22 -05:00
|
|
|
recLog("recLoop() -- start", share);
|
|
|
|
|
2022-12-13 20:27:32 -05:00
|
|
|
if (!exists("/tmp/mow-lock"))
|
2022-12-04 15:13:39 -05:00
|
|
|
{
|
2022-12-11 10:25:22 -05:00
|
|
|
recLog("/tmp/mow-lock not found, assuming it is safe to update the webroot page.", share);
|
|
|
|
recLog("webroot page = " + cleanDir(share->webRoot) + "/index.html", share);
|
2022-12-13 20:27:32 -05:00
|
|
|
|
2022-12-04 15:13:39 -05:00
|
|
|
system("touch /tmp/mow-lock");
|
2022-12-13 20:27:32 -05:00
|
|
|
|
2022-12-11 10:25:22 -05:00
|
|
|
genCSS(share);
|
|
|
|
genHTMLul(share->webRoot, string(APP_NAME) + " " + string(APP_VER), share);
|
2022-12-13 20:27:32 -05:00
|
|
|
|
|
|
|
remove("/tmp/mow-lock");
|
2022-12-04 15:13:39 -05:00
|
|
|
}
|
2022-12-11 10:25:22 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
recLog("/tmp/mow-lock pesent, skipping update of the webroot page.", share);
|
|
|
|
}
|
2022-09-23 21:50:06 -04:00
|
|
|
|
2022-12-11 12:33:56 -05:00
|
|
|
genHTMLul(share->outDir, share->camName, share);
|
2022-12-13 20:27:32 -05:00
|
|
|
|
2022-12-11 12:33:56 -05:00
|
|
|
recLog("camera specific webroot page updated. page = " + share->outDir + "/index.html", share);
|
|
|
|
|
2022-09-22 20:57:46 -04:00
|
|
|
auto bufPath = cleanDir(share->buffDir) + "/%03d." + share->vidExt;
|
|
|
|
auto secs = to_string(share->secs);
|
|
|
|
auto limSecs = to_string(share->secs + 3);
|
|
|
|
auto cmd = "timeout -k 1 " + limSecs + " ffmpeg -hide_banner -i " + share->recordUrl + " -y -vcodec copy -map 0 -segment_time 00:00:10 -f segment -t " + secs + " " + bufPath;
|
2022-08-12 21:46:36 -04:00
|
|
|
|
2022-09-22 20:57:46 -04:00
|
|
|
thread th2(detectLoop, share);
|
2022-08-12 21:46:36 -04:00
|
|
|
|
2022-12-13 20:27:32 -05:00
|
|
|
recLog("detectLoop() -- started in a seperate thread.", share);
|
|
|
|
recLog("ffmpeg_run: " + cmd, share);
|
|
|
|
|
|
|
|
auto retCode = system(cmd.c_str());
|
2022-12-11 10:25:22 -05:00
|
|
|
|
2022-12-13 20:27:32 -05:00
|
|
|
recLog("ffmpeg_retcode: " + to_string(retCode), share);
|
2022-12-11 10:25:22 -05:00
|
|
|
|
2022-12-11 12:33:56 -05:00
|
|
|
share->recLoopWait = true;
|
2022-08-12 21:46:36 -04:00
|
|
|
|
2022-12-11 12:33:56 -05:00
|
|
|
th2.join();
|
2022-07-28 10:30:07 -04:00
|
|
|
|
2022-09-22 20:57:46 -04:00
|
|
|
if (!share->skipCmd)
|
2022-08-12 21:46:36 -04:00
|
|
|
{
|
2022-12-13 20:27:32 -05:00
|
|
|
recLog("motion not detected by the detection loop.", share);
|
|
|
|
|
|
|
|
if (share->postCmd.empty())
|
|
|
|
{
|
|
|
|
recLog("post command not defined, skipping.", share);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
recLog("running post command = " + share->postCmd, share);
|
|
|
|
system(share->postCmd.c_str());
|
|
|
|
}
|
2022-08-12 21:46:36 -04:00
|
|
|
}
|
2022-12-11 10:25:22 -05:00
|
|
|
else
|
|
|
|
{
|
2022-12-13 20:27:32 -05:00
|
|
|
recLog("motion detected by the detection loop, skipping the post command.", share);
|
2022-12-11 10:25:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
recLog("recLoop() -- finished", share);
|
|
|
|
|
2022-12-13 20:27:32 -05:00
|
|
|
if (share->retCode != 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-07-08 15:24:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2022-09-22 20:57:46 -04:00
|
|
|
struct shared_t sharedRes;
|
|
|
|
|
2022-07-08 15:24:45 -04:00
|
|
|
sharedRes.conf = parseForParam("-c", argc, argv, false);
|
|
|
|
|
|
|
|
if (parseForParam("-h", argc, argv, true) == "true")
|
2022-04-14 09:45:54 -04:00
|
|
|
{
|
2022-09-23 21:50:06 -04:00
|
|
|
cout << "Motion Watch " << APP_VER << endl << endl;
|
2022-09-22 20:57:46 -04:00
|
|
|
cout << "Usage: mow <argument>" << endl << endl;
|
|
|
|
cout << "-h : display usage information about this application." << endl;
|
|
|
|
cout << "-c : path to the config file." << endl;
|
2022-09-23 21:50:06 -04:00
|
|
|
cout << "-v : display the current version." << endl;
|
|
|
|
}
|
2022-12-04 15:13:39 -05:00
|
|
|
else if (parseForParam("-v", argc, argv, true) == "true")
|
2022-09-23 21:50:06 -04:00
|
|
|
{
|
|
|
|
cout << APP_VER << endl;
|
2022-04-14 09:45:54 -04:00
|
|
|
}
|
2022-07-08 15:24:45 -04:00
|
|
|
else if (sharedRes.conf.empty())
|
2022-04-14 09:45:54 -04:00
|
|
|
{
|
2022-08-12 21:46:36 -04:00
|
|
|
cerr << "err: A config file was not given in -c" << endl;
|
2022-04-14 09:45:54 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-09-22 20:57:46 -04:00
|
|
|
sharedRes.retCode = 0;
|
|
|
|
sharedRes.recLoopWait = false;
|
|
|
|
sharedRes.skipCmd = false;
|
|
|
|
sharedRes.init = true;
|
2022-12-11 10:25:22 -05:00
|
|
|
sharedRes.logRun = true;
|
|
|
|
|
2022-12-04 15:13:39 -05:00
|
|
|
recLoop(&sharedRes);
|
2022-04-14 09:45:54 -04:00
|
|
|
|
2022-07-08 15:24:45 -04:00
|
|
|
return sharedRes.retCode;
|
2022-04-14 09:45:54 -04:00
|
|
|
}
|
|
|
|
|
2022-07-08 15:24:45 -04:00
|
|
|
return EINVAL;
|
2022-04-14 09:45:54 -04:00
|
|
|
}
|