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.

364 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
6 years ago
​
​
​
6 years ago
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
6 years ago
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolZLMEDIA.h"
  2. #include "../json/json_tool.h"
  3. #include <json/value.h>
  4. #include <json/writer.h>
  5. #include <json/reader.h>
  6. #define LOG( X ) basicLogCollection::getInstance()->Log( "ZLMP", this, X )
  7. #define LOG_ERROR( X ) basicLogCollection::getInstance()->Log( "ZLMP", this, _LOG_ERROR, X )
  8. protocolZLMEDIA::protocolZLMEDIA( config_setting_t *Settings ): protocolHTTP( basicSettingsProvider::getInstance()->getSubNode(Settings, "ZLMEDIA_PROTOCOL") ){
  9. m_pLog = basicLogCollection::getInstance();
  10. m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "ZLMEDIA_PROTOCOL");
  11. /*
  12. if( m_pSettings != NULL ){
  13. config_setting_t *pSetting;
  14. if (!config_setting_lookup_string(m_pSettings, "SCANNER_NAME", (const char**)&m_pScannerName)){
  15. strncpy( m_pScannerName, "LRF_2D_001", 64 );
  16. LOG( m_pScannerName );
  17. pSetting = config_setting_add(m_pSettings, "SCANNER_NAME", CONFIG_TYPE_STRING);
  18. config_setting_set_string(pSetting, m_pScannerName);
  19. basicSettingsProvider::getInstance()->compromise();
  20. };
  21. if (!config_setting_lookup_int(m_pSettings, "SCANNER_DIMENTIONS", &m_nScannerDimentions )){
  22. m_nScannerDimentions = 2;
  23. pSetting = config_setting_add(m_pSettings, "SCANNER_DIMENTIONS", CONFIG_TYPE_INT);
  24. config_setting_set_int(pSetting, m_nScannerDimentions);
  25. basicSettingsProvider::getInstance()->compromise();
  26. };
  27. if (!config_setting_lookup_string(m_pSettings, "LCU_IP_ADDRESS", (const char**)&m_pServerIP)){
  28. strncpy( m_pServerIP, "192.168.200.204", 16 );
  29. LOG( m_pServerIP );
  30. pSetting = config_setting_add(m_pSettings, "LCU_IP_ADDRESS", CONFIG_TYPE_STRING);
  31. config_setting_set_string(pSetting, m_pServerIP);
  32. basicSettingsProvider::getInstance()->compromise();
  33. };
  34. if (!config_setting_lookup_int(m_pSettings, "LCU_IP_PORT", &m_nServerPort)){
  35. m_nServerPort = 80;
  36. pSetting = config_setting_add(m_pSettings, "LCU_IP_PORT", CONFIG_TYPE_INT);
  37. config_setting_set_int(pSetting, m_nServerPort);
  38. basicSettingsProvider::getInstance()->compromise();
  39. };
  40. basicSettingsProvider::getInstance()->validate();
  41. }; //*/
  42. };
  43. protocolZLMEDIA::protocolZLMEDIA( CSTR key, CSTR host, CSTR ip, uint32_t port ): protocolHTTP( NULL ){
  44. m_pLog = basicLogCollection::getInstance();
  45. m_pSettings = NULL;
  46. strncpy( m_pSecret, key, 255 );
  47. strncpy( m_pVHost, host, 255 );
  48. strncpy( m_pAccessPoint, key, 255 );
  49. strncpy( m_pIP, ip, 16 );
  50. LOG("Done!");
  51. m_bSessionOnGo = false;
  52. m_bServerIsPresent = false;
  53. m_nPort = port;
  54. m_bUseSSL = ( m_nPort == 433 );
  55. LOG("Check Online!");
  56. isOnline();
  57. LOG("Done!");
  58. };
  59. protocolZLMEDIA::~protocolZLMEDIA(){
  60. };
  61. bool protocolZLMEDIA::packetHandler( protocolPackage *Package ){
  62. //LOG("Pack Handler!!");
  63. if( Package == NULL ){
  64. m_bSessionOnGo = false;
  65. return false;
  66. };
  67. //LOG("Pack not NULL");
  68. if( Package->m_nSize == 0 ) {
  69. m_bSessionOnGo = false;
  70. return false;
  71. };
  72. //LOG("Size not NULL");
  73. if( Package->m_pData == NULL ){
  74. m_bSessionOnGo = false;
  75. return false;
  76. };
  77. //LOG("Data not NULL");
  78. if( Package->m_nCode != 200 ){
  79. m_bSessionOnGo = false;
  80. return false;
  81. };
  82. LOG("Code is 200");
  83. m_bServerIsPresent = true;
  84. try{
  85. Json::Value root;
  86. Json::Reader reader;
  87. if( !reader.parse( (char *)Package->m_pData, root ) ){
  88. LOG_ERROR( "Error parsing the string" );
  89. m_bSessionOnGo = false;
  90. return false;
  91. };
  92. for( Json::Value::const_iterator outer = root.begin() ; outer != root.end() ; outer++ ){
  93. for( Json::Value::const_iterator inner = (*outer).begin() ; inner!= (*outer).end() ; inner++ ){
  94. char tBuffer[256];
  95. sprintf( tBuffer,"Data: %s: %s ", inner.key().asString().c_str(), (*inner).asString().c_str() );
  96. LOG( tBuffer );
  97. if( strstr( inner.key().asString().c_str(), "key" ) != NULL ) {
  98. LOG("New Key Arived");
  99. strncpy( m_pLastValue, (*inner).asString().c_str(), 255 );
  100. };
  101. };
  102. };
  103. } catch (exception e ){
  104. LOG_ERROR("Exception!!");
  105. m_bSessionOnGo = false;
  106. return false;
  107. };
  108. m_bSessionOnGo = false;
  109. return true;
  110. };
  111. // Issue New Online Check but return current value
  112. bool protocolZLMEDIA::isOnline(){
  113. if( !m_bServerIsPresent ) { // Just First Time to set initial flag
  114. if( !m_bSessionOnGo ) {
  115. char URL[256];
  116. uint8_t timeout = 100; // 1 sec.
  117. m_bSessionOnGo = true;
  118. if( m_bUseSSL ) {
  119. sprintf(URL,"https://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_API, m_pSecret );
  120. } else {
  121. sprintf(URL,"http://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_API, m_pSecret ); //m_pAccessPoint
  122. };
  123. LOG( URL );
  124. //ToDo: Check if OK to send nothing for checking Onlines
  125. //int len = 0;
  126. //issuePost( URL, NULL, len );
  127. issueGet( URL );
  128. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  129. };
  130. #ifdef DEBUG
  131. if(m_bServerIsPresent) {
  132. LOG("Server is Online");
  133. } else {
  134. LOG("Server is Onffline");
  135. };
  136. #endif
  137. };
  138. return m_bServerIsPresent;
  139. };
  140. bool protocolZLMEDIA::restartServer(){
  141. return false;
  142. };
  143. bool protocolZLMEDIA::getConfig(){
  144. return false;
  145. };
  146. /* Converts a hex character to its integer value */
  147. char from_hex(char ch) {
  148. return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
  149. }
  150. /* Converts an integer value to its hex character*/
  151. char to_hex(char code) {
  152. static char hex[] = "0123456789abcdef";
  153. return hex[code & 15];
  154. }
  155. /* Returns a url-encoded version of str */
  156. /* IMPORTANT: be sure to free() the returned string after use */
  157. char *url_encode(char *str) {
  158. char *pstr = str, *buf = (char *) malloc(strlen(str) * 3 + 1), *pbuf = buf;
  159. while (*pstr) {
  160. if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
  161. *pbuf++ = *pstr;
  162. else if (*pstr == ' ')
  163. *pbuf++ = '+';
  164. else
  165. *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
  166. pstr++;
  167. }
  168. *pbuf = '\0';
  169. return buf;
  170. }
  171. /* Returns a url-decoded version of str */
  172. /* IMPORTANT: be sure to free() the returned string after use */
  173. char *url_decode(char *str) {
  174. char *pstr = str, *buf = (char *)malloc(strlen(str) + 1), *pbuf = buf;
  175. while (*pstr) {
  176. if (*pstr == '%') {
  177. if (pstr[1] && pstr[2]) {
  178. *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
  179. pstr += 2;
  180. }
  181. } else if (*pstr == '+') {
  182. *pbuf++ = ' ';
  183. } else {
  184. *pbuf++ = *pstr;
  185. }
  186. pstr++;
  187. }
  188. *pbuf = '\0';
  189. return buf;
  190. }
  191. #include <iomanip>
  192. #include <sstream>
  193. #include <string>
  194. using namespace std;
  195. string urlEncode( const string &value ){
  196. ostringstream escaped;
  197. escaped.fill('0');
  198. escaped << hex;
  199. for( string::const_iterator i = value.begin(), n = value.end(); i != n; ++i ){
  200. string::value_type c = (*i);
  201. // Keep alphanumeric and other accepted characters intact
  202. if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
  203. escaped << c;
  204. continue;
  205. };
  206. // Any other characters are percent-encoded
  207. escaped << uppercase;
  208. escaped << '%' << setw(2) << int((unsigned char) c);
  209. escaped << nouppercase;
  210. };
  211. return escaped.str();
  212. };
  213. string urlDecode( string text ){
  214. string escaped;
  215. for( auto i = text.begin(), nd = text.end(); i < nd; ++i ){
  216. auto c = ( *i );
  217. switch( c ){
  218. case '%': if( i[1] && i[2] ){
  219. char hs[]{ i[1], i[2] };
  220. escaped += static_cast<char>(strtol(hs, nullptr, 16));
  221. i += 2;
  222. };
  223. break;
  224. case '+': escaped += ' '; break;
  225. default: escaped += c;
  226. };
  227. };
  228. return escaped;
  229. };
  230. CSTR protocolZLMEDIA::buildURL( CSTR base, uint32_t streamId, CSTR host, CSTR app, CSTR src ){
  231. char tBuffer[2048];
  232. char *a = url_encode( app );
  233. char *s = url_encode( src );
  234. char *h = url_encode( host );
  235. if( a == NULL || h == NULL || s == NULL ) {
  236. if( a != NULL ) free( a );
  237. if( s != NULL ) free ( s );
  238. if( h != NULL ) free( h );
  239. return NULL;
  240. };
  241. if( m_bUseSSL ) { //__defaultVhost__
  242. 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, base, h, a, streamId, s, m_pSecret );
  243. } else {
  244. 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, base, h, a, streamId, s, m_pSecret );
  245. };
  246. free(a); free(s); free(h);
  247. return strdup(tBuffer);
  248. };
  249. CSTR protocolZLMEDIA::addStreamProxy( uint32_t streamId, CSTR app, CSTR mrl ){
  250. if( m_bServerIsPresent ) { // Just First Time to set initial flag
  251. uint8_t timeout = 100; // 1 sec.
  252. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  253. if( !m_bSessionOnGo ){
  254. char *URL;
  255. LOG("Add Stream Proxy");
  256. m_bSessionOnGo = true;
  257. URL = buildURL( ZL_ADD_STREAM_PROXY, streamId, m_pVHost, app, mrl );
  258. LOG( URL );
  259. //ToDo: Check if OK to send nothing for checking Onlines
  260. //int len = 0;
  261. //issuePost( URL, NULL, len );
  262. issueGet( URL );
  263. free( URL );
  264. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  265. if( ! m_bSessionOnGo ) {
  266. ZL_STREAM_INFO *tVal;
  267. tVal = (ZL_STREAM_INFO*) malloc(sizeof(ZL_STREAM_INFO));
  268. if( tVal != NULL ) {
  269. tVal->streamId = streamId;
  270. strncpy( tVal->key, m_pLastValue, 128 );
  271. m_vCurrentStreams.push_back( tVal );
  272. LOG("Stream Added Successfully");
  273. LOG( m_pLastValue );
  274. return tVal->key;
  275. };
  276. return NULL;
  277. };
  278. return NULL;
  279. } else LOG("LIST MEDIA - SESSION BUSY!");
  280. };
  281. return NULL;
  282. };
  283. bool protocolZLMEDIA::delStreamProxy(CSTR key){
  284. return false;
  285. };
  286. bool protocolZLMEDIA::delAllStreamProxys( void ){
  287. if( m_bServerIsPresent ) { // Just First Time to set initial flag
  288. uint8_t timeout = 100; // 1 sec.
  289. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  290. if( !m_bSessionOnGo ){
  291. char URL[256];
  292. LOG("Restart Server");
  293. m_bSessionOnGo = true;
  294. if( m_bUseSSL ) {
  295. sprintf(URL,"https://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_RESTART_SERVER, m_pSecret );
  296. } else {
  297. sprintf(URL,"http://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_RESTART_SERVER, m_pSecret );
  298. };
  299. LOG( URL );
  300. issueGet( URL );
  301. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  302. return ( timeout > 0 );
  303. } else LOG("LIST MEDIA - SESSION BUSY!");
  304. };
  305. return false;
  306. };
  307. bool protocolZLMEDIA::listMedia(){
  308. if( m_bServerIsPresent ) { // Just First Time to set initial flag
  309. uint8_t timeout = 100; // 1 sec.
  310. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  311. if( !m_bSessionOnGo ) {
  312. char URL[256];
  313. LOG("List Media");
  314. m_bSessionOnGo = true;
  315. if( m_bUseSSL ) {
  316. sprintf(URL,"https://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_MEDIA, m_pSecret );
  317. } else {
  318. sprintf(URL,"http://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_MEDIA, m_pSecret ); //m_pAccessPoint
  319. };
  320. LOG( URL );
  321. //ToDo: Check if OK to send nothing for checking Onlines
  322. //int len = 0;
  323. //issuePost( URL, NULL, len );
  324. issueGet( URL );
  325. timeout = 100;
  326. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  327. return ( timeout > 0 );
  328. } else LOG("LIST MEDIA - SESSION BUSY!");
  329. };
  330. return false;
  331. };
  332. bool protocolZLMEDIA::requestIsFinished(){
  333. return (m_bSessionOnGo == false);
  334. };
  335. #undef LOG
  336. #undef LOG_ERROR