Skip to content

Commit

Permalink
Make it possible to reload configuration if config file changes #69
Browse files Browse the repository at this point in the history
  • Loading branch information
vmdocua committed Jan 16, 2024
1 parent 16f74b2 commit e02e220
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions Capture/videocapture/VideoCapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ namespace fs = std::filesystem;
/* ######################### begin common ############################# */

#define EX_SYS_BREAK_EXEC 140 /* custom exit code when execution broken by Ctrl+C, SIGINT or similar events */
#define EX_CONFIG_RELOAD 141 /* custom exit code when config.yaml file changed */

// private static global flag
/*static */ volatile sig_atomic_t s_nSysBreakExec = 0;
Expand Down Expand Up @@ -120,6 +121,16 @@ std::string exec(bool verbose, const std::string& cmd) {
return result;
}

// get file hash info in string format representing unique file snapshot in time
std::string getFileChangeHash(const std::string& filePath) {
std::filesystem::file_time_type mt = std::filesystem::last_write_time(filePath);
auto t = mt.time_since_epoch();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(t);
std::ostringstream oss;
oss << ms.count();
return oss.str();
}

std::vector<std::string> getVideoDevicePaths(const std::string& pattern) {
glob_t glob_result;
memset(&glob_result, 0, sizeof(glob_result));
Expand Down Expand Up @@ -680,9 +691,7 @@ std::string vdToString(VideoDevice& vd) {
return s.str();
}

////////////////////////////////////////////////////////////////////////////
// Entry point
int main(int argc, char* argv[]) {
int appRun(int argc, char* argv[]) {
AppOpts opts;
AppConfig cfg;

Expand Down Expand Up @@ -719,6 +728,7 @@ int main(int argc, char* argv[]) {
std::string targetBusInfo;
std::string targetVideoDevPath;
std::string targetAudioDevPath;
std::string configHash = getFileChangeHash(opts.configPath);

// current video signal status
MWCAP_VIDEO_SIGNAL_STATUS vssCur = {};
Expand All @@ -727,6 +737,7 @@ int main(int argc, char* argv[]) {
MWCAP_VIDEO_SIGNAL_STATUS vssPrev = {};

bool fRun = true;
bool fConfigChanged = false;
int recording = 0;
std::string init_ts;
std::string start_ts;
Expand Down Expand Up @@ -850,15 +861,38 @@ int main(int argc, char* argv[]) {
vssPrev = vssCur;
safeMWCloseChannel(hChannel);

// check config changed
std::string configHash2 = getFileChangeHash(opts.configPath);
if( configHash!=configHash2 ) {
_INFO("Config file was modified (" << configHash << " -> " << configHash2 << ") : " << opts.configPath);
_INFO("Reloading config and restarting capture ...");
fConfigChanged = true;
fRun = false;
}
} while (fRun && !isSysBreakExec());

safeStopRecording(opts, recording, start_ts, "Program terminated");

if( fInit )
MWCaptureExitInstance();

if(isSysBreakExec() )
if( isSysBreakExec() )
return EX_SYS_BREAK_EXEC;

if( fConfigChanged )
return EX_CONFIG_RELOAD;

return EX_OK;
}

////////////////////////////////////////////////////////////////////////////
// Entry point

int main(int argc, char* argv[]) {
int res = EX_OK;
do {
res = appRun(argc, argv);
optind = 0; // force restart argument scanning for getopt
} while( res==EX_CONFIG_RELOAD );
return res;
}

0 comments on commit e02e220

Please sign in to comment.