diff --git a/src/common.cpp b/src/common.cpp index b5ecb43..9948c13 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -350,8 +350,3 @@ string genVidNameFromLive(const string &tsPath) return string(); } } - -uint64_t genEpoch() -{ - return duration_cast(system_clock::now().time_since_epoch()).count(); -} diff --git a/src/common.h b/src/common.h index edd2400..46aae60 100644 --- a/src/common.h +++ b/src/common.h @@ -34,7 +34,7 @@ using namespace std; using namespace std::filesystem; using namespace std::chrono; -#define APP_VER "2.0" +#define APP_VER "2.1.t1" #define APP_NAME "Motion Watch" #define REC_LOG_NAME "rec_log_lines.html" #define DET_LOG_NAME "det_log_lines.html" @@ -44,13 +44,13 @@ struct pls_t { string evName; vector srcPaths; - uint64_t createTime; Mat thumbnail; }; struct shared_t { map recList; + string curEventName; string conf; string recLog; string detLog; @@ -64,6 +64,7 @@ struct shared_t string webFont; string webRoot; bool skipCmd; + int maxScore; int procTime; int schSec; int pixThresh; @@ -90,6 +91,5 @@ void enforceMaxEvents(shared_t *share); bool rdConf(shared_t *share); vector lsFilesInDir(const string &path, const string &ext = string()); vector lsDirsInDir(const string &path); -uint64_t genEpoch(); #endif // COMMON_H diff --git a/src/main.cpp b/src/main.cpp index c4b8d72..d3d58e7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,40 +27,30 @@ void eventLoop(shared_t *share) { while (share->retCode == 0) { - while (!share->recList.empty()) + while (share->recList.size() > 1) { - auto it = share->recList.begin(); - auto evName = it->first; - auto event = it->second; - auto timeDiff = genEpoch() - event.createTime; + auto it = share->recList.begin(); + auto evName = it->first; + auto event = it->second; - // wait at least 62 seconds before processing the event in - // queue. - if ((timeDiff > 0) && (timeDiff > 62)) + try { - try - { - createDirTree("events"); - wrOutVod(event, share); - genHTMLvod(evName); + createDirTree("events"); + wrOutVod(event, share); + genHTMLvod(evName); - if (!exists("events/" + evName + ".jpg")) - { - imwrite(string("events/" + evName + ".jpg").c_str(), event.thumbnail); - } - } - catch (filesystem_error &ex) + if (!exists("events/" + evName + ".jpg")) { - recLog(string("err: ") + ex.what(), share); + imwrite(string("events/" + evName + ".jpg").c_str(), event.thumbnail); } - - share->recList.erase(it); } - - sleep(5); + catch (filesystem_error &ex) + { + recLog(string("err: ") + ex.what(), share); + } } - sleep(5); + sleep(10); } } @@ -126,7 +116,7 @@ void recLoop(shared_t *share) " -hls_segment_filename 'live/%Y-%j-%H-%M-%S.ts'" + " -hls_flags delete_segments" + " -y -vcodec copy" + - " -f hls -hls_time 10 -hls_list_size 400" + + " -f hls -hls_time 2 -hls_list_size 1000" + " stream.m3u8"; recLog("ffmpeg_run: " + cmd, share); @@ -170,6 +160,7 @@ int main(int argc, char** argv) { sharedRes.retCode = 0; sharedRes.procTime = 0; + sharedRes.maxScore = 0; sharedRes.skipCmd = false; rdConf(&sharedRes); diff --git a/src/mo_detect.cpp b/src/mo_detect.cpp index 90e5586..6b2cb60 100644 --- a/src/mo_detect.cpp +++ b/src/mo_detect.cpp @@ -16,6 +16,11 @@ void detectMoInStream(const string &streamFile, shared_t *share) { if (share->procTime >= share->schSec) { + share->curEventName.clear(); + + share->procTime = 0; + share->maxScore = 0; + if (!share->skipCmd) { detLog("no motion detected, running post command: " + share->postCmd, share); @@ -24,7 +29,6 @@ void detectMoInStream(const string &streamFile, shared_t *share) else { share->skipCmd = false; - share->procTime = 0; detLog("motion detected, skipping the post command.", share); } @@ -42,15 +46,23 @@ void detectMoInStream(const string &streamFile, shared_t *share) } } + if (share->curEventName.empty()) + { + share->curEventName = genTimeStr("%Y-%j-%H-%M"); + } + if (!tsPath.empty()) { if (moDetect(tsPath, thumbnail, share)) { - auto eventName = genTimeStr("%Y-%j-%H-%M"); - - if (share->recList.find(eventName) != share->recList.end()) + if (share->recList.find(share->curEventName) != share->recList.end()) { - share->recList[eventName].srcPaths.push_back(tsPath); + share->recList[share->curEventName].srcPaths.push_back(tsPath); + + if (!thumbnail.empty()) + { + share->recList[share->curEventName].thumbnail = thumbnail.clone(); + } } else { @@ -58,17 +70,16 @@ void detectMoInStream(const string &streamFile, shared_t *share) event.srcPaths.push_back(tsPath); - event.createTime = genEpoch(); - event.thumbnail = thumbnail.clone(); - event.evName = eventName; + event.thumbnail = thumbnail.clone(); + event.evName = share->curEventName; - share->recList.insert(pair{eventName, event}); + share->recList.insert(pair{event.evName, event}); } share->skipCmd = true; } - share->procTime += 10; + share->procTime += 2; } } @@ -87,15 +98,14 @@ bool imgDiff(const Mat &prev, const Mat &next, int &score, shared_t *share) score = countNonZero(diff); - detLog("diff_score: " + to_string(score) + " tresh: " + to_string(share->imgThresh), share); + detLog("diff_score: " + to_string(score) + " thresh: " + to_string(share->imgThresh), share); return score >= share->imgThresh; } bool moDetect(const string &buffFile, Mat &vidThumb, shared_t *share) { - auto maxScore = 0; - auto score = 0; + auto score = 0; detLog("stream_clip: " + buffFile, share); @@ -122,9 +132,9 @@ bool moDetect(const string &buffFile, Mat &vidThumb, shared_t *share) { if (imgDiff(prev, next, score, share)) { - if (score > maxScore) + if (score > share->maxScore) { - maxScore = score; + share->maxScore = score; resize(next, vidThumb, Size(720, 480), INTER_LINEAR); } @@ -145,7 +155,7 @@ bool moDetect(const string &buffFile, Mat &vidThumb, shared_t *share) capture.release(); - return maxScore > 0; + return score > 0; } void wrOutVod(const pls_t &event, shared_t *share)