// This file is part of Motion Watch. // 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. // 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. #include "mo_detect.h" #include "logger.h" void detectLoop(shared_t *share) { detLog("detect_loop() -- start", share); vector bufFiles; auto waitingForFiles = 0; 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 if (exists(fullPath)) { remove(fullPath); } } else { if (waitingForFiles >= share->secs) { detLog("timed out waiting for buff files from ffmpeg, did it fail?", share); break; } else { detLog(to_string(bufFiles.size()) + " buff file(s) found, waiting for more.", share); } sleep(5); waitingForFiles += 5; } } while (!share->recLoopWait || !bufFiles.empty()); detLog("detect_loop() -- 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); 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 " + share->vidCodec + " -movflags faststart -map 0 -segment_time 00:00:10 -f segment -t " + secs + " " + bufPath; thread th2(detectLoop, share); recLog("detect_loop() -- started in a seperate thread.", share); recLog("ffmpeg_run: " + cmd, share); auto retCode = system(cmd.c_str()); recLog("ffmpeg_retcode: " + to_string(retCode), share); share->recLoopWait = true; th2.join(); recLog("detect_loop() -- thread finished.", share); if (!share->skipCmd) { 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()); } } else { recLog("motion detected by the detection loop, 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") { cout << "Motion Watch " << APP_VER << endl << endl; cout << "Usage: mow " << 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; } else if (sharedRes.conf.empty()) { cerr << "err: A config file was not given in -c" << endl; } else { sharedRes.retCode = 0; sharedRes.recLoopWait = false; sharedRes.skipCmd = false; sharedRes.init = true; sharedRes.logRun = true; recLoop(&sharedRes); return sharedRes.retCode; } return EINVAL; }