You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

422 lines
15 KiB

#include "protocolZLMEDIA.h"
#include "../json/json_tool.h"
#include <json/value.h>
#include <json/writer.h>
#include <json/reader.h>
#define LOG( X ) basicLogCollection::getInstance()->Log( "ZLMP", this, X )
#define LOG_ERROR( X ) basicLogCollection::getInstance()->Log( "ZLMP", this, _LOG_ERROR, X )
protocolZLMEDIA::protocolZLMEDIA( config_setting_t *Settings ): protocolHTTP( basicSettingsProvider::getInstance()->getSubNode(Settings, "ZLMEDIA_PROTOCOL") ){
m_pLog = basicLogCollection::getInstance();
m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "ZLMEDIA_PROTOCOL");
/*
if( m_pSettings != NULL ){
config_setting_t *pSetting;
if (!config_setting_lookup_string(m_pSettings, "SCANNER_NAME", (const char**)&m_pScannerName)){
strncpy( m_pScannerName, "LRF_2D_001", 64 );
LOG( m_pScannerName );
pSetting = config_setting_add(m_pSettings, "SCANNER_NAME", CONFIG_TYPE_STRING);
config_setting_set_string(pSetting, m_pScannerName);
basicSettingsProvider::getInstance()->compromise();
};
if (!config_setting_lookup_int(m_pSettings, "SCANNER_DIMENTIONS", &m_nScannerDimentions )){
m_nScannerDimentions = 2;
pSetting = config_setting_add(m_pSettings, "SCANNER_DIMENTIONS", CONFIG_TYPE_INT);
config_setting_set_int(pSetting, m_nScannerDimentions);
basicSettingsProvider::getInstance()->compromise();
};
if (!config_setting_lookup_string(m_pSettings, "LCU_IP_ADDRESS", (const char**)&m_pServerIP)){
strncpy( m_pServerIP, "192.168.200.204", 16 );
LOG( m_pServerIP );
pSetting = config_setting_add(m_pSettings, "LCU_IP_ADDRESS", CONFIG_TYPE_STRING);
config_setting_set_string(pSetting, m_pServerIP);
basicSettingsProvider::getInstance()->compromise();
};
if (!config_setting_lookup_int(m_pSettings, "LCU_IP_PORT", &m_nServerPort)){
m_nServerPort = 80;
pSetting = config_setting_add(m_pSettings, "LCU_IP_PORT", CONFIG_TYPE_INT);
config_setting_set_int(pSetting, m_nServerPort);
basicSettingsProvider::getInstance()->compromise();
};
basicSettingsProvider::getInstance()->validate();
}; //*/
};
protocolZLMEDIA::protocolZLMEDIA( CSTR key, CSTR host, CSTR ip, uint32_t port ): protocolHTTP( NULL ){
m_pLog = basicLogCollection::getInstance();
m_pSettings = NULL;
strncpy( m_pSecret, key, 255 );
strncpy( m_pVHost, host, 255 );
strncpy( m_pAccessPoint, key, 255 );
strncpy( m_pIP, ip, 16 );
LOG("Done!");
m_bSessionOnGo = false;
m_bServerIsPresent = false;
m_nPort = port;
m_bUseSSL = ( m_nPort == 433 );
LOG("Check Online!");
isOnline();
LOG("Done!");
};
protocolZLMEDIA::~protocolZLMEDIA(){
};
bool protocolZLMEDIA::packetHandler( protocolPackage *Package ){
//LOG("Pack Handler!!");
if( Package == NULL ){
m_bSessionOnGo = false;
return false;
};
//LOG("Pack not NULL");
if( Package->m_nSize == 0 ) {
m_bSessionOnGo = false;
return false;
};
//LOG("Size not NULL");
if( Package->m_pData == NULL ){
m_bSessionOnGo = false;
return false;
};
//LOG("Data not NULL");
if( Package->m_nCode != 200 ){
m_bSessionOnGo = false;
return false;
};
LOG("Code is 200");
m_bServerIsPresent = true;
try{
Json::Value root;
#ifdef OLD_WAY_JSON
Json::Reader reader;
if( !reader.parse( (char *)Package->m_pData, root ) ){
LOG_ERROR( "Error parsing the string" );
m_bSessionOnGo = false;
return false;
};
#else
JSONCPP_STRING err;
Json::CharReaderBuilder builder;
const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
if (!reader->parse((char *)Package->m_pData, (char *)Package->m_pData + Package->m_nSize, &root, &err)) {
LOG_ERROR( "Error parsing the string" );
LOG_ERROR( err.c_str() );
m_bSessionOnGo = false;
return false;
};
#endif
for( Json::Value::const_iterator outer = root.begin() ; outer != root.end() ; outer++ ){
for( Json::Value::const_iterator inner = (*outer).begin() ; inner!= (*outer).end() ; inner++ ){
char tBuffer[256];
sprintf( tBuffer,"Data: %s: %s ", inner.key().asString().c_str(), (*inner).asString().c_str() );
LOG( tBuffer );
if( strstr( inner.key().asString().c_str(), "key" ) != NULL ) {
LOG("New Key Arived");
strncpy( m_pLastValue, (*inner).asString().c_str(), 255 );
};
};
};
} catch (exception e ){
LOG_ERROR("Exception!!");
m_bSessionOnGo = false;
return false;
};
m_bSessionOnGo = false;
return true;
};
// Issue New Online Check but return current value
bool protocolZLMEDIA::isOnline(){
if( !m_bServerIsPresent ) { // Just First Time to set initial flag
if( !m_bSessionOnGo ) {
char URL[256];
uint8_t timeout = 100; // 1 sec.
m_bSessionOnGo = true;
if( m_bUseSSL ) {
sprintf(URL,"https://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_API, m_pSecret );
} else {
sprintf(URL,"http://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_API, m_pSecret ); //m_pAccessPoint
};
LOG( URL );
//ToDo: Check if OK to send nothing for checking Onlines
//int len = 0;
//issuePost( URL, NULL, len );
issueGet( URL );
while( m_bSessionOnGo && timeout-- ) SLEEP(10);
};
#ifdef DEBUG
if(m_bServerIsPresent) {
LOG("Server is Online");
} else {
LOG("Server is Onffline");
};
#endif
};
return m_bServerIsPresent;
};
bool protocolZLMEDIA::restartServer(){
return false;
};
bool protocolZLMEDIA::getConfig(){
return false;
};
/* Converts a hex character to its integer value */
char from_hex(char ch) {
return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
}
/* Converts an integer value to its hex character*/
char to_hex(char code) {
static char hex[] = "0123456789abcdef";
return hex[code & 15];
}
/* Returns a url-encoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */
char *url_encode(char *str) {
char *pstr = str, *buf = (char *) malloc(strlen(str) * 3 + 1), *pbuf = buf;
while (*pstr) {
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
*pbuf++ = *pstr;
else if (*pstr == ' ')
*pbuf++ = '+';
else
*pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
pstr++;
}
*pbuf = '\0';
return buf;
}
/* Returns a url-decoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */
char *url_decode(char *str) {
char *pstr = str, *buf = (char *)malloc(strlen(str) + 1), *pbuf = buf;
while (*pstr) {
if (*pstr == '%') {
if (pstr[1] && pstr[2]) {
*pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
pstr += 2;
}
} else if (*pstr == '+') {
*pbuf++ = ' ';
} else {
*pbuf++ = *pstr;
}
pstr++;
}
*pbuf = '\0';
return buf;
}
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
string urlEncode( const string &value ){
ostringstream escaped;
escaped.fill('0');
escaped << hex;
for( string::const_iterator i = value.begin(), n = value.end(); i != n; ++i ){
string::value_type c = (*i);
// Keep alphanumeric and other accepted characters intact
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
escaped << c;
continue;
};
// Any other characters are percent-encoded
escaped << uppercase;
escaped << '%' << setw(2) << int((unsigned char) c);
escaped << nouppercase;
};
return escaped.str();
};
string urlDecode( string text ){
string escaped;
for( auto i = text.begin(), nd = text.end(); i < nd; ++i ){
auto c = ( *i );
switch( c ){
case '%': if( i[1] && i[2] ){
char hs[]{ i[1], i[2] };
escaped += static_cast<char>(strtol(hs, nullptr, 16));
i += 2;
};
break;
case '+': escaped += ' '; break;
default: escaped += c;
};
};
return escaped;
};
CSTR protocolZLMEDIA::buildURL( uint8_t CMD, uint32_t streamId, CSTR host, CSTR app, CSTR src ){
char tBuffer[2048];
char *a = url_encode( app );
char *s = url_encode( src );
char *h = url_encode( host );
if( a == NULL || h == NULL || s == NULL ) {
if( a != NULL ) free( a );
if( s != NULL ) free ( s );
if( h != NULL ) free( h );
return NULL;
};
switch ( CMD ) {
case 1:{
sprintf( tBuffer,"https://%s:%d/%s?&vhost=%s&app=%s&stream=%d&url=%s&enable_hls=1&rtp_type=0&secret=%s", m_pIP, m_nPort, ZL_ADD_ENCODER_SOURCE, h, a, streamId, s, m_pSecret );
break;
};
case 2:{
if( m_bUseSSL ) { //__defaultVhost__
sprintf( tBuffer,"https://%s:%d/%s?&vhost=%s&app=%s&stream=%d&url=%s&enable_hls=1&rtp_type=0&secret=%s", m_pIP, m_nPort, ZL_ADD_STREAM_PROXY, h, a, streamId, s, m_pSecret );
} else {
sprintf( tBuffer, "http://%s:%d/%s?&vhost=%s&app=%s&stream=%d&url=%s&enable_hls=1&rtp_type=0&secret=%s", m_pIP, m_nPort, ZL_ADD_STREAM_PROXY, h, a, streamId, s, m_pSecret );
};
break;
};
};
free(a); free(s); free(h);
return strdup(tBuffer);
};
CSTR protocolZLMEDIA::addStreamProxy( uint32_t streamId, CSTR app, CSTR mrl ){
if( m_bServerIsPresent ) { // Just First Time to set initial flag
uint8_t timeout = 100; // 1 sec.
while( m_bSessionOnGo && timeout-- ) SLEEP(10);
if( !m_bSessionOnGo ){
char *URL;
LOG("Add Stream Proxy");
m_bSessionOnGo = true;
URL = buildURL( ZL_CMD_ADD_PROXY, streamId, m_pVHost, app, mrl );
LOG( URL );
//ToDo: Check if OK to send nothing for checking Onlines
//int len = 0;
//issuePost( URL, NULL, len );
issueGet( URL );
free( URL );
while( m_bSessionOnGo && timeout-- ) SLEEP(10);
if( ! m_bSessionOnGo ) {
ZL_STREAM_INFO *tVal;
tVal = (ZL_STREAM_INFO*) malloc(sizeof(ZL_STREAM_INFO));
if( tVal != NULL ) {
tVal->streamId = streamId;
strncpy( tVal->key, m_pLastValue, 128 );
m_vCurrentStreams.push_back( tVal );
LOG("Stream Added Successfully");
LOG( m_pLastValue );
return tVal->key;
};
return NULL;
};
return NULL;
} else LOG("LIST MEDIA - SESSION BUSY!");
};
return NULL;
};
CSTR protocolZLMEDIA::addFFmpegSource( uint32_t streamId, CSTR app, CSTR mrl ){
if( m_bServerIsPresent ) { // Just First Time to set initial flag
uint8_t timeout = 100; // 1 sec.
while( m_bSessionOnGo && timeout-- ) SLEEP(10);
if( !m_bSessionOnGo ){
char *URL;
LOG("Add FFmpeg Source");
m_bSessionOnGo = true;
URL = buildURL( ZL_CMD_ADD_FFMPEG, streamId, m_pVHost, app, mrl );
LOG( URL );
//ToDo: Check if OK to send nothing for checking Onlines
//int len = 0;
//issuePost( URL, NULL, len );
issueGet( URL );
free( URL );
while( m_bSessionOnGo && timeout-- ) SLEEP(10);
if( ! m_bSessionOnGo ) {
ZL_STREAM_INFO *tVal;
tVal = (ZL_STREAM_INFO*) malloc(sizeof(ZL_STREAM_INFO));
if( tVal != NULL ) {
tVal->streamId = streamId;
strncpy( tVal->key, m_pLastValue, 128 );
m_vCurrentStreams.push_back( tVal );
LOG("Stream Added Successfully");
LOG( m_pLastValue );
return tVal->key;
};
return NULL;
};
return NULL;
} else LOG("LIST MEDIA - SESSION BUSY!");
};
return NULL;
};
bool protocolZLMEDIA::delStreamProxy(CSTR key){
return false;
};
bool protocolZLMEDIA::delAllStreamProxys( void ){
if( m_bServerIsPresent ) { // Just First Time to set initial flag
uint8_t timeout = 100; // 1 sec.
while( m_bSessionOnGo && timeout-- ) SLEEP(10);
if( !m_bSessionOnGo ){
char URL[256];
LOG("Restart Server");
m_bSessionOnGo = true;
if( m_bUseSSL ) {
sprintf(URL,"https://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_RESTART_SERVER, m_pSecret );
} else {
sprintf(URL,"http://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_RESTART_SERVER, m_pSecret );
};
LOG( URL );
issueGet( URL );
while( m_bSessionOnGo && timeout-- ) SLEEP(10);
return ( timeout > 0 );
} else LOG("LIST MEDIA - SESSION BUSY!");
};
return false;
};
bool protocolZLMEDIA::listMedia(){
if( m_bServerIsPresent ) { // Just First Time to set initial flag
uint8_t timeout = 100; // 1 sec.
while( m_bSessionOnGo && timeout-- ) SLEEP(10);
if( !m_bSessionOnGo ) {
char URL[256];
LOG("List Media");
m_bSessionOnGo = true;
if( m_bUseSSL ) {
sprintf(URL,"https://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_MEDIA, m_pSecret );
} else {
sprintf(URL,"http://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_MEDIA, m_pSecret ); //m_pAccessPoint
};
LOG( URL );
//ToDo: Check if OK to send nothing for checking Onlines
//int len = 0;
//issuePost( URL, NULL, len );
issueGet( URL );
timeout = 100;
while( m_bSessionOnGo && timeout-- ) SLEEP(10);
return ( timeout > 0 );
} else LOG("LIST MEDIA - SESSION BUSY!");
};
return false;
};
bool protocolZLMEDIA::requestIsFinished(){
return (m_bSessionOnGo == false);
};
#undef LOG
#undef LOG_ERROR