From e8f1ceff3a7c8007d3739505874c212b3011aaa9 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Tue, 17 Apr 2018 16:27:29 +0200 Subject: [PATCH] Generalized stream variables implementation --- lib/stream.cpp | 134 ++++++++++++++++--------------------------------- lib/stream.h | 1 + 2 files changed, 45 insertions(+), 90 deletions(-) diff --git a/lib/stream.cpp b/lib/stream.cpp index 69e8af42..0d70097d 100644 --- a/lib/stream.cpp +++ b/lib/stream.cpp @@ -16,11 +16,47 @@ #include "dtsc.h" #include "triggers.h"//LTS -/* roxlu-begin */ -static std::string strftime_now(const std::string& format); -static void replace(std::string& str, const std::string& from, const std::string& to); -static void replace_variables(std::string& str); -/* roxlu-end */ +/// Calls strftime using the current local time, returning empty string on any error. +static std::string strftime_now(const std::string& format) { + time_t rawtime; + char buffer[80]; + time(&rawtime); + struct tm* timeinfo = localtime(&rawtime); + if (!timeinfo || !strftime(buffer, 80, format.c_str(), timeinfo)){return "";} + return buffer; +} + +///Replaces any occurrences of 'from' with 'to' in 'str'. +static void replace(std::string& str, const std::string& from, const std::string& to) { + if (from.empty()){return;} + size_t start_pos = 0; + while((start_pos = str.find(from, start_pos)) != std::string::npos) { + str.replace(start_pos, from.length(), to); + start_pos += to.length(); + } +} + +///Replaces all stream-related variables in the given 'str' with their values. +void Util::streamVariables(std::string &str, const std::string & streamname, const std::string & source) { + replace(str, "$source", source); + replace(str, "$datetime", "$year.$month.$day.$hour.$minute.$second"); + replace(str, "$day", strftime_now("%d")); + replace(str, "$month", strftime_now("%m")); + replace(str, "$year", strftime_now("%Y")); + replace(str, "$hour", strftime_now("%H")); + replace(str, "$minute", strftime_now("%M")); + replace(str, "$second", strftime_now("%S")); + replace(str, "$stream", streamname); + if (streamname.find('+') != std::string::npos){ + std::string strbase = streamname.substr(0, streamname.find('+')); + std::string strext = streamname.substr(streamname.find('+')+1); + replace(str, "$basename", strbase); + replace(str, "$wildcard", strext); + }else{ + replace(str, "$basename", streamname); + replace(str, "$wildcard", ""); + } +} std::string Util::getTmpFolder() { std::string dir; @@ -203,6 +239,8 @@ bool Util::startInput(std::string streamname, std::string filename, bool forkFir filename = stream_cfg.getMember("source").asString(); } + streamVariables(filename, streamname); + //check in curConf for capabilities-inputs--priority/source_match std::string player_bin; bool selected = false; @@ -364,17 +402,7 @@ pid_t Util::startPush(const std::string & streamname, std::string & target) { } // The target can hold variables like current time etc - replace_variables(target); - replace(target, "$stream", streamname); - if (streamname.find('+') != std::string::npos){ - std::string strbase = streamname.substr(0, streamname.find('+')); - std::string strext = streamname.substr(streamname.find('+')+1); - replace(target, "$basename", strbase); - replace(target, "$wildcard", strext); - }else{ - replace(target, "$basename", streamname); - replace(target, "$wildcard", ""); - } + streamVariables(target, streamname); //Attempt to load up configuration and find this stream IPC::sharedPage mistConfOut(SHM_CONF, DEFAULT_CONF_PAGE_SIZE); @@ -424,80 +452,6 @@ pid_t Util::startPush(const std::string & streamname, std::string & target) { } -static void replace(std::string& str, const std::string& from, const std::string& to) { - if(from.empty()) { - return; - } - size_t start_pos = 0; - while((start_pos = str.find(from, start_pos)) != std::string::npos) { - str.replace(start_pos, from.length(), to); - start_pos += to.length(); - } -} - -static void replace_variables(std::string& str) { - - char buffer[80] = { 0 }; - std::map vars; - std::string day = strftime_now("%d"); - std::string month = strftime_now("%m"); - std::string year = strftime_now("%Y"); - std::string hour = strftime_now("%H"); - std::string minute = strftime_now("%M"); - std::string seconds = strftime_now("%S"); - std::string datetime = year +"." +month +"." +day +"." +hour +"." +minute +"." +seconds; - - if (0 == day.size()) { - WARN_MSG("Failed to retrieve the current day with strftime_now()."); - } - if (0 == month.size()) { - WARN_MSG("Failed to retrieve the current month with strftime_now()."); - } - if (0 == year.size()) { - WARN_MSG("Failed to retrieve the current year with strftime_now()."); - } - if (0 == hour.size()) { - WARN_MSG("Failed to retrieve the current hour with strftime_now()."); - } - if (0 == minute.size()) { - WARN_MSG("Failed to retrieve the current minute with strftime_now()."); - } - if (0 == seconds.size()) { - WARN_MSG("Failed to retrieve the current seconds with strftime_now()."); - } - - vars.insert(std::pair("$day", day)); - vars.insert(std::pair("$month", month)); - vars.insert(std::pair("$year", year)); - vars.insert(std::pair("$hour", hour)); - vars.insert(std::pair("$minute", minute)); - vars.insert(std::pair("$second", seconds)); - vars.insert(std::pair("$datetime", datetime)); - - std::map::iterator it = vars.begin(); - while (it != vars.end()) { - replace(str, it->first, it->second); - ++it; - } -} - -static std::string strftime_now(const std::string& format) { - - time_t rawtime; - struct tm* timeinfo = NULL; - char buffer [80] = { 0 }; - - time(&rawtime); - timeinfo = localtime (&rawtime); - - if (0 == strftime(buffer, 80, format.c_str(), timeinfo)) { - FAIL_MSG("Call to stftime() failed with format: %s, maybe our buffer is not big enough (80 bytes).", format.c_str()); - return ""; - } - - return buffer; -} - uint8_t Util::getStreamStatus(const std::string & streamname){ char pageName[NAME_BUFFER_SIZE]; snprintf(pageName, NAME_BUFFER_SIZE, SHM_STREAM_STATE, streamname.c_str()); diff --git a/lib/stream.h b/lib/stream.h index 8264a1d0..2a0f33be 100644 --- a/lib/stream.h +++ b/lib/stream.h @@ -7,6 +7,7 @@ #include "json.h" namespace Util { + void streamVariables(std::string &str, const std::string & streamname, const std::string & source = ""); std::string getTmpFolder(); void sanitizeName(std::string & streamname); bool streamAlive(std::string & streamname);