The error checking with ffmpeg is not working. Learned that it doesn't
always return 0 on success. Decided to remove the error checking
altogether. Instead ffmpeg failures should be checked manually using
stderr.

Dirent includes .. and . so I decided to switch to the filesystem entry
listing that should hopefully exclude those special directories.

The camera webroot was not generating .index files. those files would
only get generated if motion was detected. Copied the code that does
that onto recLoop() to execute regardless of motion.
This commit is contained in:
Maurice ONeal 2022-12-11 12:33:56 -05:00
parent baeaabbd55
commit 5ab50433cf
5 changed files with 25 additions and 75 deletions

View File

@ -57,44 +57,21 @@ bool fileExists(const string& name)
return access(name.c_str(), F_OK) != -1; return access(name.c_str(), F_OK) != -1;
} }
string replaceAll(string str, const string &from, const string &to)
{
if (from.empty()) return str;
size_t startPos = 0;
while ((startPos = str.find(from, startPos)) != string::npos)
{
str.replace(startPos, from.length(), to);
startPos += to.length();
}
return str;
}
vector<string> lsFilesInDir(const string &path, const string &ext) vector<string> lsFilesInDir(const string &path, const string &ext)
{ {
DIR *dir;
struct dirent *ent;
vector<string> names; vector<string> names;
if ((dir = opendir(path.c_str())) != NULL) for (auto &entry : directory_iterator(path))
{ {
while ((ent = readdir(dir)) != NULL) if (entry.is_regular_file())
{ {
if (ent->d_type & DT_REG) auto name = entry.path().filename().string();
{
auto name = string(ent->d_name);
if (name.ends_with(ext.c_str()) || ext.empty()) if (ext.empty() || name.ends_with(ext))
{ {
names.push_back(name); names.push_back(name);
}
} }
} }
closedir(dir);
} }
sort(names.begin(), names.end()); sort(names.begin(), names.end());
@ -104,26 +81,14 @@ vector<string> lsFilesInDir(const string &path, const string &ext)
vector<string> lsDirsInDir(const string &path) vector<string> lsDirsInDir(const string &path)
{ {
DIR *dir;
struct dirent *ent;
vector<string> names; vector<string> names;
if ((dir = opendir(path.c_str())) != NULL) for (auto &entry : directory_iterator(path))
{ {
while ((ent = readdir(dir)) != NULL) if (entry.is_directory())
{ {
if (ent->d_type & DT_DIR) names.push_back(entry.path().filename().string());
{
auto name = string(ent->d_name);
if ((name != "..") || (name != "."))
{
names.push_back(name);
}
}
} }
closedir(dir);
} }
sort(names.begin(), names.end()); sort(names.begin(), names.end());

View File

@ -22,7 +22,6 @@
#include <errno.h> #include <errno.h>
#include <vector> #include <vector>
#include <thread> #include <thread>
#include <dirent.h>
#include <filesystem> #include <filesystem>
#include <mutex> #include <mutex>
#include <sys/types.h> #include <sys/types.h>
@ -36,7 +35,7 @@ using namespace cv;
using namespace std; using namespace std;
using namespace std::filesystem; using namespace std::filesystem;
#define APP_VER "1.5.t3" #define APP_VER "1.5.t4"
#define APP_NAME "Motion Watch" #define APP_NAME "Motion Watch"
struct shared_t struct shared_t
@ -72,7 +71,6 @@ string genDstFile(const string &dirOut, const char *fmt, const string &e
string genTimeStr(const char *fmt); string genTimeStr(const char *fmt);
string cleanDir(const string &path); string cleanDir(const string &path);
string parseForParam(const string &arg, int argc, char** argv, bool argOnly); string parseForParam(const string &arg, int argc, char** argv, bool argOnly);
string replaceAll(string str, const string &from, const string &to);
bool createDir(const string &dir); bool createDir(const string &dir);
bool createDirTree(const string &full_path); bool createDirTree(const string &full_path);
bool fileExists(const string& name); bool fileExists(const string& name);

View File

@ -46,7 +46,6 @@ void logLoop(shared_t *share)
htmlText += "<html>\n"; htmlText += "<html>\n";
htmlText += "<head>\n"; htmlText += "<head>\n";
htmlText += "<link rel='stylesheet' href='/theme.css'>\n"; htmlText += "<link rel='stylesheet' href='/theme.css'>\n";
htmlText += "<meta http-equiv='refresh' content='10'>";
htmlText += "</head>\n"; htmlText += "</head>\n";
htmlText += "<body>\n"; htmlText += "<body>\n";
htmlText += "<p>\n"; htmlText += "<p>\n";

View File

@ -52,7 +52,7 @@ void detectLoop(shared_t *share)
sleep(1); sleep(1);
} }
} }
while (!bufFiles.empty()); while (!bufFiles.empty() && !share->recLoopWait);
detLog("detectLoop() -- finished", share); detLog("detectLoop() -- finished", share);
} }
@ -77,6 +77,9 @@ void recLoop(shared_t *share)
recLog("/tmp/mow-lock pesent, skipping update of the webroot page.", share); recLog("/tmp/mow-lock pesent, skipping update of the webroot page.", share);
} }
genHTMLul(share->outDir, share->camName, share);
recLog("camera specific webroot page updated. page = " + share->outDir + "/index.html", share);
auto bufPath = cleanDir(share->buffDir) + "/%03d." + share->vidExt; auto bufPath = cleanDir(share->buffDir) + "/%03d." + share->vidExt;
auto secs = to_string(share->secs); auto secs = to_string(share->secs);
auto limSecs = to_string(share->secs + 3); auto limSecs = to_string(share->secs + 3);
@ -87,26 +90,11 @@ void recLoop(shared_t *share)
recLog("detect_loop started in a seperate thread.", share); recLog("detect_loop started in a seperate thread.", share);
recLog("ffmpeg = " + cmd, share); recLog("ffmpeg = " + cmd, share);
auto ret = system(cmd.c_str()); system(cmd.c_str());
if (ret == 0) share->recLoopWait = true;
{
recLog("ffmpeg_return_code = " + to_string(ret), share);
share->recLoopWait = true; th2.join();
th2.join();
}
else
{
recLog("ffmpeg failed, cooling down for " + to_string(share->secs) + "secs.", share);
recLog("ffmpeg_return_code = " + to_string(ret), share);
// simulate that ffmpeg is running even after it has failed.
sleep(share->secs);
share->recLoopWait = true;
}
if (!share->skipCmd) if (!share->skipCmd)
{ {

View File

@ -14,8 +14,6 @@
void genHTMLul(const string &outputDir, const string &title, shared_t *share) void genHTMLul(const string &outputDir, const string &title, shared_t *share)
{ {
DIR *dir;
struct dirent *ent;
vector<string> logNames; vector<string> logNames;
vector<string> regNames = lsFilesInDir(outputDir); vector<string> regNames = lsFilesInDir(outputDir);
vector<string> dirNames = lsDirsInDir(outputDir); vector<string> dirNames = lsDirsInDir(outputDir);
@ -82,24 +80,23 @@ void genHTMLul(const string &outputDir, const string &title, shared_t *share)
void genHTMLvid(const string &outputVid, shared_t *share) void genHTMLvid(const string &outputVid, shared_t *share)
{ {
auto filename = path(outputVid).filename().string(); auto vidName = path(outputVid).filename().string();
auto filePath = path(outputVid).parent_path().string(); auto filePath = path(outputVid).parent_path().string();
auto fileName = vidName.substr(0, vidName.size() - (share->vidExt.size() + 1));
string htmlText = "<!DOCTYPE html>\n"; string htmlText = "<!DOCTYPE html>\n";
filename = replaceAll(filename, share->vidExt, "html");
htmlText += "<html>\n"; htmlText += "<html>\n";
htmlText += "<head>\n"; htmlText += "<head>\n";
htmlText += "<link rel='stylesheet' href='/theme.css'>\n"; htmlText += "<link rel='stylesheet' href='/theme.css'>\n";
htmlText += "</head>\n"; htmlText += "</head>\n";
htmlText += "<body>\n"; htmlText += "<body>\n";
htmlText += "<video width=100% height=100% controls autoplay>\n"; htmlText += "<video width=100% height=100% controls autoplay>\n";
htmlText += " <source src='" + filename + "' type='video/" + share->vidExt + "'>\n"; htmlText += " <source src='" + vidName + "' type='video/" + share->vidExt + "'>\n";
htmlText += "</video>\n"; htmlText += "</video>\n";
htmlText += "</body>\n"; htmlText += "</body>\n";
htmlText += "</html>"; htmlText += "</html>";
ofstream file(string(cleanDir(filePath) + "/" + filename + ".html").c_str()); ofstream file(string(filePath + "/" + fileName + ".html").c_str());
file << htmlText << endl; file << htmlText << endl;
@ -113,7 +110,10 @@ void genCSS(shared_t *share)
cssText += " background-color: " + share->webBg + ";\n"; cssText += " background-color: " + share->webBg + ";\n";
cssText += " color: " + share->webTxt + ";\n"; cssText += " color: " + share->webTxt + ";\n";
cssText += " font-family: " + share->webFont + ";\n"; cssText += " font-family: " + share->webFont + ";\n";
cssText += "}"; cssText += "}\n";
cssText += "a {\n";
cssText += " color: " + share->webTxt + ";\n";
cssText += "}\n";
ofstream file(string(cleanDir(share->webRoot) + "/theme.css").c_str()); ofstream file(string(cleanDir(share->webRoot) + "/theme.css").c_str());