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
|
|
|
|
2023-02-05 14:05:56 -05:00
|
|
|
void detectMoInFile(const string &detPath, const string &recPath, shared_t *share)
|
2022-04-14 09:45:54 -04:00
|
|
|
{
|
2022-12-21 20:30:37 -05:00
|
|
|
detLog("detect_mo_in_file() -- start", share);
|
2022-12-11 10:25:22 -05:00
|
|
|
|
2022-12-21 20:30:37 -05:00
|
|
|
Mat thumbNail;
|
2022-04-14 09:45:54 -04:00
|
|
|
|
2023-02-05 14:05:56 -05:00
|
|
|
if (moDetect(detPath, thumbNail, share))
|
2022-08-12 21:46:36 -04:00
|
|
|
{
|
2022-12-21 20:30:37 -05:00
|
|
|
share->skipCmd = true;
|
2022-08-12 21:46:36 -04:00
|
|
|
|
2023-02-05 14:05:56 -05:00
|
|
|
wrOut(recPath, thumbNail, share);
|
2022-12-21 20:30:37 -05:00
|
|
|
}
|
2023-02-05 14:05:56 -05:00
|
|
|
|
|
|
|
if (exists(detPath)) remove(detPath);
|
|
|
|
if (exists(recPath)) remove(recPath);
|
|
|
|
|
|
|
|
detLog("detect_mo_in_file() -- finished", share);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *runDetCmd(void *arg)
|
|
|
|
{
|
|
|
|
auto share = reinterpret_cast<shared_t*>(arg);
|
|
|
|
|
|
|
|
recLog("ffmpeg_det_run: " + share->detCmd, share);
|
|
|
|
pclose(popen(share->detCmd.c_str(), "r"));
|
|
|
|
|
|
|
|
share->cmdFinished++; return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *runRecCmd(void *arg)
|
|
|
|
{
|
|
|
|
auto share = reinterpret_cast<shared_t*>(arg);
|
|
|
|
|
|
|
|
if (share->recordUrl != share->detectUrl)
|
2022-12-21 20:30:37 -05:00
|
|
|
{
|
2023-02-05 14:05:56 -05:00
|
|
|
// recording command is not allowed to run if both
|
|
|
|
// streams are the same.
|
|
|
|
recLog("ffmpeg_rec_run: " + share->recCmd, share);
|
|
|
|
pclose(popen(share->recCmd.c_str(), "r"));
|
2022-04-14 09:45:54 -04:00
|
|
|
}
|
2022-12-11 10:25:22 -05:00
|
|
|
|
2023-02-05 14:05:56 -05:00
|
|
|
share->cmdFinished++; return NULL;
|
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-16 18:24:18 -05:00
|
|
|
recLog("rec_loop() -- start", share);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (!exists("/tmp/mow-lock"))
|
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-16 18:24:18 -05:00
|
|
|
recLog("webroot page updated: " + cleanDir(share->webRoot) + "/index.html", share);
|
2022-12-04 15:13:39 -05:00
|
|
|
}
|
2022-12-11 10:25:22 -05:00
|
|
|
else
|
|
|
|
{
|
2022-12-16 18:24:18 -05:00
|
|
|
recLog("skipping update of the webroot page, it is busy.", share);
|
2022-12-11 10:25:22 -05:00
|
|
|
}
|
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-16 18:24:18 -05:00
|
|
|
recLog("camera specific webroot page updated: " + share->outDir + "/index.html", share);
|
2022-12-11 12:33:56 -05:00
|
|
|
|
2022-12-24 13:48:51 -05:00
|
|
|
for (auto i = 0; i < share->numOfClips; ++i)
|
2022-12-21 20:30:37 -05:00
|
|
|
{
|
2023-02-05 14:05:56 -05:00
|
|
|
auto detPath = cleanDir(share->buffDir) + "/" + to_string(i) + share->detSuffix;
|
|
|
|
auto recPath = cleanDir(share->buffDir) + "/" + to_string(i) + share->recSuffix;
|
2022-12-22 20:15:14 -05:00
|
|
|
|
2023-02-05 14:05:56 -05:00
|
|
|
if (share->recordUrl == share->detectUrl)
|
|
|
|
{
|
|
|
|
recPath = detPath;
|
|
|
|
}
|
2022-08-12 21:46:36 -04:00
|
|
|
|
2023-02-05 14:05:56 -05:00
|
|
|
share->recCmd = "ffmpeg -hide_banner -i " + share->recordUrl + " -y -vcodec " + share->vidCodec + " -t " + to_string(share->clipLen) + " " + recPath;
|
|
|
|
share->detCmd = "ffmpeg -hide_banner -i " + share->detectUrl + " -y -vcodec " + share->vidCodec + " -t " + to_string(share->clipLen) + " " + detPath;
|
|
|
|
|
|
|
|
share->cmdFinished = 0;
|
2022-08-12 21:46:36 -04:00
|
|
|
|
2023-02-05 14:05:56 -05:00
|
|
|
pthread_t detThr;
|
|
|
|
pthread_t recThr;
|
2022-12-13 20:27:32 -05:00
|
|
|
|
2023-02-05 14:05:56 -05:00
|
|
|
auto detOk = pthread_create(&detThr, NULL, &runDetCmd, share);
|
|
|
|
auto recOk = pthread_create(&recThr, NULL, &runRecCmd, share);
|
2022-12-11 10:25:22 -05:00
|
|
|
|
2023-02-05 14:05:56 -05:00
|
|
|
if (detOk == 0 && recOk == 0)
|
2022-12-21 20:30:37 -05:00
|
|
|
{
|
2023-02-05 14:05:56 -05:00
|
|
|
sleep(share->clipLen);
|
2022-12-11 10:25:22 -05:00
|
|
|
|
2023-02-05 14:05:56 -05:00
|
|
|
if (share->cmdFinished < 2)
|
|
|
|
{
|
|
|
|
// 2 second grace period.
|
|
|
|
sleep(2);
|
|
|
|
}
|
2022-08-12 21:46:36 -04:00
|
|
|
|
2023-02-05 14:05:56 -05:00
|
|
|
if (share->cmdFinished < 2)
|
2022-12-21 20:30:37 -05:00
|
|
|
{
|
2023-02-05 14:05:56 -05:00
|
|
|
// force close ffmpeg cmds after failing to finish
|
|
|
|
// after the grace period.
|
|
|
|
pthread_cancel(detThr);
|
|
|
|
pthread_cancel(recThr);
|
2022-12-21 20:30:37 -05:00
|
|
|
}
|
2022-12-21 20:44:58 -05:00
|
|
|
|
2023-02-05 14:05:56 -05:00
|
|
|
new thread(detectMoInFile, detPath, recPath, share);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
recLog("could not start one or both ffmpeg cmds in seperate threads. detOk = " + to_string(detOk) + " recOk = " + to_string(recOk), share);
|
2022-12-21 20:30:37 -05:00
|
|
|
}
|
|
|
|
}
|
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-21 20:30:37 -05:00
|
|
|
recLog("no motion detected", share);
|
2022-12-13 20:27:32 -05:00
|
|
|
|
|
|
|
if (share->postCmd.empty())
|
|
|
|
{
|
|
|
|
recLog("post command not defined, skipping.", share);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-16 18:24:18 -05:00
|
|
|
recLog("running post command: " + share->postCmd, share);
|
2022-12-13 20:27:32 -05:00
|
|
|
system(share->postCmd.c_str());
|
|
|
|
}
|
2022-08-12 21:46:36 -04:00
|
|
|
}
|
2022-12-11 10:25:22 -05:00
|
|
|
else
|
|
|
|
{
|
2022-12-21 20:30:37 -05:00
|
|
|
recLog("motion detected, skipping the post command.", share);
|
2022-12-11 10:25:22 -05:00
|
|
|
}
|
|
|
|
|
2022-12-16 18:24:18 -05:00
|
|
|
recLog("rec_loop() -- finished", share);
|
2022-12-11 10:25:22 -05:00
|
|
|
|
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-12-24 13:48:51 -05:00
|
|
|
sharedRes.conf = parseForList("-c", argc, argv);
|
2022-07-08 15:24:45 -04:00
|
|
|
|
|
|
|
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;
|
2022-12-24 13:48:51 -05:00
|
|
|
cout << "-c : path to a config file." << endl;
|
|
|
|
cout << "-v : display the current version." << endl << endl;
|
|
|
|
cout << "note: multiple -c config files can be passed, reading from left" << endl;
|
|
|
|
cout << " to right. any conflicting values between the files will" << endl;
|
|
|
|
cout << " have the latest value from the latest file overwrite the" << endl;
|
|
|
|
cout << " the earliest." << endl;
|
2022-09-23 21:50:06 -04:00
|
|
|
}
|
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-12-24 13:48:51 -05:00
|
|
|
cerr << "err: no config file(s) were given in -c" << endl;
|
2022-04-14 09:45:54 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-12-21 20:30:37 -05:00
|
|
|
sharedRes.retCode = 0;
|
|
|
|
sharedRes.skipCmd = false;
|
|
|
|
sharedRes.init = true;
|
2022-12-11 10:25:22 -05:00
|
|
|
|
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
|
|
|
}
|