JustMotion/src/main.cpp

133 lines
3.8 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"
void detectLoop(shared_t *share)
2022-04-14 09:45:54 -04:00
{
vector<string> bufFiles;
2022-04-14 09:45:54 -04:00
do
{
bufFiles = lsFilesInDir(share->buffDir, "." + share->vidExt);
// 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.
if ((bufFiles.size() >= 2) || (share->recLoopWait && !bufFiles.empty()))
{
auto fullPath = cleanDir(share->buffDir) + "/" + bufFiles[0];
Mat thumbNail;
if (moDetect(fullPath, thumbNail, share))
{
share->skipCmd = true;
wrOut(fullPath, thumbNail, share);
}
else
{
remove(fullPath.c_str());
2022-04-14 09:45:54 -04:00
}
}
else
{
sleep(1);
}
2022-04-14 09:45:54 -04:00
}
while (!bufFiles.empty());
}
void recLoop(shared_t *share)
{
while (rdConf(share))
{
if (!fileExists("/tmp/mow-lock"))
{
system("touch /tmp/mow-lock");
auto webRoot = path(share->outDir).parent_path().string();
genHTMLul(webRoot, string(APP_NAME) + " " + string(APP_VER));
system("rm /tmp/mow-lock");
}
createDirTree(share->buffDir);
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;
thread th2(detectLoop, share);
if (system(cmd.c_str()) != 0)
{
share->recLoopWait = true;
th2.join();
}
else
{
// simulate that ffmpeg is running even after it has failed.
sleep(share->secs);
share->recLoopWait = true;
}
if (!share->skipCmd)
{
system(share->postCmd.c_str());
}
}
}
int main(int argc, char** argv)
{
struct shared_t sharedRes;
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;
}
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: A config file was not given in -c" << endl;
2022-04-14 09:45:54 -04:00
}
else
{
sharedRes.retCode = 0;
sharedRes.recLoopWait = false;
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
}