JustMotion/src/main.cpp

162 lines
4.6 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 &bufPath, shared_t *share)
2022-04-14 09:45:54 -04:00
{
detLog("detect_mo_in_file() -- start", share);
Mat thumbNail;
2022-04-14 09:45:54 -04:00
if (moDetect(bufPath, thumbNail, share))
{
share->skipCmd = true;
wrOut(bufPath, thumbNail, share);
}
else if (exists(bufPath))
{
remove(bufPath);
2022-04-14 09:45:54 -04:00
}
detLog("detect_mo_in_file() -- finished", share);
}
void recLoop(shared_t *share)
{
while (rdConf(share))
{
recLog("rec_loop() -- start", share);
enforceMaxLogSize(share->recLogPath, share);
enforceMaxLogSize(share->detLogPath, share);
initLogFile(share->recLogPath, share->recLogFile);
initLogFile(share->detLogPath, share->detLogFile);
initLogFrontPages(share);
if (!exists("/tmp/mow-lock"))
{
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);
}
else
{
recLog("skipping update of the webroot page, it is busy.", share);
}
genHTMLul(share->outDir, share->camName, share);
recLog("camera specific webroot page updated: " + share->outDir + "/index.html", share);
for (auto i = 0; i < share->secs; i += 10)
{
auto bufPath = cleanDir(share->buffDir) + "/" + to_string(i) + "." + share->vidExt;
auto cmd = "ffmpeg -hide_banner -i " + share->recordUrl + " -y vcodec " + share->vidCodec + " -movflags faststart -t 10 " + bufPath;
recLog("ffmpeg_run: " + cmd, share);
auto retCode = system(cmd.c_str());
recLog("ffmpeg_retcode: " + to_string(retCode), share);
if (retCode == 0)
{
recLog("detect_mo_in_file() -- started in a seperate thread.", share);
share->detThreads.push_back(thread(detectMoInFile, bufPath, share));
}
else
{
recLog("ffmpeg returned non zero, indicating failure. please check stderr output.", share);
if (exists(bufPath))
{
remove(bufPath);
}
}
}
waitForDetThreads(share);
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(share->postCmd.c_str());
}
}
else
{
recLog("motion detected, skipping the post command.", share);
}
recLog("rec_loop() -- finished", share);
if (share->retCode != 0)
{
break;
}
}
}
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.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
}