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.

560 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolZLMEDIA.h"
  2. #include "../json/json_tool.h"
  3. #include <json/reader.h>
  4. #include <json/value.h>
  5. #include <json/writer.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) :
  9. protocolHTTP(basicSettingsProvider::getInstance()->getSubNode(Settings, "ZLMEDIA_PROTOCOL"))
  10. {
  11. m_pLog = basicLogCollection::getInstance();
  12. m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "ZLMEDIA_PROTOCOL");
  13. /*
  14. if( m_pSettings != NULL ){
  15. config_setting_t *pSetting;
  16. if (!config_setting_lookup_string(m_pSettings, "SCANNER_NAME", (const char**)&m_pScannerName)){
  17. strncpy( m_pScannerName, "LRF_2D_001", 64 );
  18. LOG( m_pScannerName );
  19. pSetting = config_setting_add(m_pSettings, "SCANNER_NAME", CONFIG_TYPE_STRING);
  20. config_setting_set_string(pSetting, m_pScannerName);
  21. basicSettingsProvider::getInstance()->compromise();
  22. };
  23. if (!config_setting_lookup_int(m_pSettings, "SCANNER_DIMENTIONS", &m_nScannerDimentions )){
  24. m_nScannerDimentions = 2;
  25. pSetting = config_setting_add(m_pSettings, "SCANNER_DIMENTIONS", CONFIG_TYPE_INT);
  26. config_setting_set_int(pSetting, m_nScannerDimentions);
  27. basicSettingsProvider::getInstance()->compromise();
  28. };
  29. if (!config_setting_lookup_string(m_pSettings, "LCU_IP_ADDRESS", (const char**)&m_pServerIP)){
  30. strncpy( m_pServerIP, "192.168.200.204", 16 );
  31. LOG( m_pServerIP );
  32. pSetting = config_setting_add(m_pSettings, "LCU_IP_ADDRESS", CONFIG_TYPE_STRING);
  33. config_setting_set_string(pSetting, m_pServerIP);
  34. basicSettingsProvider::getInstance()->compromise();
  35. };
  36. if (!config_setting_lookup_int(m_pSettings, "LCU_IP_PORT", &m_nServerPort)){
  37. m_nServerPort = 80;
  38. pSetting = config_setting_add(m_pSettings, "LCU_IP_PORT", CONFIG_TYPE_INT);
  39. config_setting_set_int(pSetting, m_nServerPort);
  40. basicSettingsProvider::getInstance()->compromise();
  41. };
  42. basicSettingsProvider::getInstance()->validate();
  43. }; //*/
  44. }
  45. protocolZLMEDIA::protocolZLMEDIA(CSTR key, CSTR host, CSTR ip, uint32_t port) :
  46. protocolHTTP(NULL)
  47. {
  48. m_pLog = basicLogCollection::getInstance();
  49. m_pSettings = NULL;
  50. strncpy(m_pSecret, key, 255);
  51. strncpy(m_pVHost, host, 255);
  52. strncpy(m_pAccessPoint, key, 255);
  53. strncpy(m_pIP, ip, 16);
  54. LOG("Done!");
  55. m_bSessionOnGo = false;
  56. m_bServerIsPresent = false;
  57. m_nPort = port;
  58. m_bUseSSL = (m_nPort == 433);
  59. LOG("Check Online!");
  60. isOnline();
  61. LOG("Done!");
  62. }
  63. protocolZLMEDIA::~protocolZLMEDIA() {}
  64. bool protocolZLMEDIA::packetHandler(protocolPackage * Package)
  65. {
  66. // LOG("Pack Handler!!");
  67. if (Package == NULL)
  68. {
  69. m_bSessionOnGo = false;
  70. return false;
  71. };
  72. // LOG("Pack not NULL");
  73. if (Package->m_nSize == 0)
  74. {
  75. m_bSessionOnGo = false;
  76. return false;
  77. };
  78. // LOG("Size not NULL");
  79. if (Package->m_pData == NULL)
  80. {
  81. m_bSessionOnGo = false;
  82. return false;
  83. };
  84. // LOG("Data not NULL");
  85. if (Package->m_nCode != 200)
  86. {
  87. m_bSessionOnGo = false;
  88. return false;
  89. };
  90. LOG("Code is 200");
  91. m_bServerIsPresent = true;
  92. try
  93. {
  94. Json::Value root;
  95. #ifdef OLD_WAY_JSON
  96. Json::Reader reader;
  97. if (! reader.parse((char *) Package->m_pData, root))
  98. {
  99. LOG_ERROR("Error parsing the string");
  100. m_bSessionOnGo = false;
  101. return false;
  102. };
  103. #else
  104. JSONCPP_STRING err;
  105. Json::CharReaderBuilder builder;
  106. const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
  107. if (! reader->parse((char *) Package->m_pData, (char *) Package->m_pData + Package->m_nSize, &root, &err))
  108. {
  109. LOG_ERROR("Error parsing the string");
  110. LOG_ERROR(err.c_str());
  111. m_bSessionOnGo = false;
  112. return false;
  113. };
  114. #endif
  115. for (Json::Value::const_iterator outer = root.begin(); outer != root.end(); outer++)
  116. {
  117. for (Json::Value::const_iterator inner = (*outer).begin(); inner != (*outer).end(); inner++)
  118. {
  119. char tBuffer[256];
  120. sprintf(tBuffer, "Data: %s: %s ", inner.key().asString().c_str(), (*inner).asString().c_str());
  121. LOG(tBuffer);
  122. if (strstr(inner.key().asString().c_str(), "key") != NULL)
  123. {
  124. LOG("New Key Arived");
  125. strncpy(m_pLastValue, (*inner).asString().c_str(), 255);
  126. };
  127. };
  128. };
  129. }
  130. catch (exception e)
  131. {
  132. LOG_ERROR("Exception!!");
  133. m_bSessionOnGo = false;
  134. return false;
  135. };
  136. m_bSessionOnGo = false;
  137. return true;
  138. }
  139. // Issue New Online Check but return current value
  140. bool protocolZLMEDIA::isOnline()
  141. {
  142. if (! m_bServerIsPresent)
  143. { // Just First Time to set initial flag
  144. if (! m_bSessionOnGo)
  145. {
  146. char URL[256];
  147. uint8_t timeout = 100; // 1 sec.
  148. m_bSessionOnGo = true;
  149. if (m_bUseSSL)
  150. {
  151. sprintf(URL, "https://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_API, m_pSecret);
  152. }
  153. else
  154. {
  155. sprintf(URL, "http://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_API, m_pSecret); // m_pAccessPoint
  156. };
  157. LOG(URL);
  158. // ToDo: Check if OK to send nothing for checking Onlines
  159. // int len = 0;
  160. // issuePost( URL, NULL, len );
  161. issueGet(URL);
  162. while (m_bSessionOnGo && timeout--)
  163. {
  164. SLEEP(10);
  165. }
  166. };
  167. #ifdef DEBUG
  168. if (m_bServerIsPresent)
  169. {
  170. LOG("Server is Online");
  171. }
  172. else
  173. {
  174. LOG("Server is Onffline");
  175. };
  176. #endif
  177. };
  178. return m_bServerIsPresent;
  179. }
  180. bool protocolZLMEDIA::restartServer()
  181. {
  182. return false;
  183. }
  184. bool protocolZLMEDIA::getConfig()
  185. {
  186. return false;
  187. }
  188. /* Converts a hex character to its integer value */
  189. char from_hex(char ch)
  190. {
  191. return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
  192. }
  193. /* Converts an integer value to its hex character*/
  194. char to_hex(char code)
  195. {
  196. static char hex[] = "0123456789abcdef";
  197. return hex[code & 15];
  198. }
  199. /* Returns a url-encoded version of str */
  200. /* IMPORTANT: be sure to free() the returned string after use */
  201. char * url_encode(char * str)
  202. {
  203. char *pstr = str, *buf = (char *) malloc(strlen(str) * 3 + 1), *pbuf = buf;
  204. while (*pstr)
  205. {
  206. if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
  207. {
  208. *pbuf++ = *pstr;
  209. }
  210. else if (*pstr == ' ')
  211. {
  212. *pbuf++ = '+';
  213. }
  214. else
  215. {
  216. *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
  217. }
  218. pstr++;
  219. }
  220. *pbuf = '\0';
  221. return buf;
  222. }
  223. /* Returns a url-decoded version of str */
  224. /* IMPORTANT: be sure to free() the returned string after use */
  225. char * url_decode(char * str)
  226. {
  227. char *pstr = str, *buf = (char *) malloc(strlen(str) + 1), *pbuf = buf;
  228. while (*pstr)
  229. {
  230. if (*pstr == '%')
  231. {
  232. if (pstr[1] && pstr[2])
  233. {
  234. *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
  235. pstr += 2;
  236. }
  237. }
  238. else if (*pstr == '+')
  239. {
  240. *pbuf++ = ' ';
  241. }
  242. else
  243. {
  244. *pbuf++ = *pstr;
  245. }
  246. pstr++;
  247. }
  248. *pbuf = '\0';
  249. return buf;
  250. }
  251. #include <iomanip>
  252. #include <sstream>
  253. #include <string>
  254. using namespace std;
  255. string urlEncode(const string & value)
  256. {
  257. ostringstream escaped;
  258. escaped.fill('0');
  259. escaped << hex;
  260. for (string::const_iterator i = value.begin(), n = value.end(); i != n; ++i)
  261. {
  262. string::value_type c = (*i);
  263. // Keep alphanumeric and other accepted characters intact
  264. if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
  265. {
  266. escaped << c;
  267. continue;
  268. };
  269. // Any other characters are percent-encoded
  270. escaped << uppercase;
  271. escaped << '%' << setw(2) << int((unsigned char) c);
  272. escaped << nouppercase;
  273. };
  274. return escaped.str();
  275. }
  276. string urlDecode(string text)
  277. {
  278. string escaped;
  279. for (auto i = text.begin(), nd = text.end(); i < nd; ++i)
  280. {
  281. auto c = (*i);
  282. switch (c)
  283. {
  284. case '%':
  285. if (i[1] && i[2])
  286. {
  287. char hs[]{i[1], i[2]};
  288. escaped += static_cast<char>(strtol(hs, nullptr, 16));
  289. i += 2;
  290. };
  291. break;
  292. case '+': escaped += ' '; break;
  293. default : escaped += c;
  294. };
  295. };
  296. return escaped;
  297. }
  298. CSTR protocolZLMEDIA::buildURL(uint8_t CMD, uint32_t streamId, CSTR host, CSTR app, CSTR src)
  299. {
  300. char tBuffer[2048];
  301. char * a = url_encode(app);
  302. char * s = url_encode(src);
  303. char * h = url_encode(host);
  304. if (a == NULL || h == NULL || s == NULL)
  305. {
  306. if (a != NULL)
  307. {
  308. free(a);
  309. }
  310. if (s != NULL)
  311. {
  312. free(s);
  313. }
  314. if (h != NULL)
  315. {
  316. free(h);
  317. }
  318. return NULL;
  319. };
  320. switch (CMD)
  321. {
  322. case 1:
  323. {
  324. 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);
  325. break;
  326. };
  327. case 2:
  328. {
  329. if (m_bUseSSL)
  330. { //__defaultVhost__
  331. 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);
  332. }
  333. else
  334. {
  335. 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);
  336. };
  337. break;
  338. };
  339. };
  340. free(a);
  341. free(s);
  342. free(h);
  343. return strdup(tBuffer);
  344. }
  345. CSTR protocolZLMEDIA::addStreamProxy(uint32_t streamId, CSTR app, CSTR mrl)
  346. {
  347. if (m_bServerIsPresent)
  348. { // Just First Time to set initial flag
  349. uint8_t timeout = 100; // 1 sec.
  350. while (m_bSessionOnGo && timeout--)
  351. {
  352. SLEEP(10);
  353. }
  354. if (! m_bSessionOnGo)
  355. {
  356. char * URL;
  357. LOG("Add Stream Proxy");
  358. m_bSessionOnGo = true;
  359. URL = buildURL(ZL_CMD_ADD_PROXY, streamId, m_pVHost, app, mrl);
  360. LOG(URL);
  361. // ToDo: Check if OK to send nothing for checking Onlines
  362. // int len = 0;
  363. // issuePost( URL, NULL, len );
  364. issueGet(URL);
  365. free(URL);
  366. while (m_bSessionOnGo && timeout--)
  367. {
  368. SLEEP(10);
  369. }
  370. if (! m_bSessionOnGo)
  371. {
  372. ZL_STREAM_INFO * tVal;
  373. tVal = (ZL_STREAM_INFO *) malloc(sizeof(ZL_STREAM_INFO));
  374. if (tVal != NULL)
  375. {
  376. tVal->streamId = streamId;
  377. strncpy(tVal->key, m_pLastValue, 128);
  378. m_vCurrentStreams.push_back(tVal);
  379. LOG("Stream Added Successfully");
  380. LOG(m_pLastValue);
  381. return tVal->key;
  382. };
  383. return NULL;
  384. };
  385. return NULL;
  386. }
  387. else
  388. {
  389. LOG("LIST MEDIA - SESSION BUSY!");
  390. }
  391. };
  392. return NULL;
  393. }
  394. CSTR protocolZLMEDIA::addFFmpegSource(uint32_t streamId, CSTR app, CSTR mrl)
  395. {
  396. if (m_bServerIsPresent)
  397. { // Just First Time to set initial flag
  398. uint8_t timeout = 100; // 1 sec.
  399. while (m_bSessionOnGo && timeout--)
  400. {
  401. SLEEP(10);
  402. }
  403. if (! m_bSessionOnGo)
  404. {
  405. char * URL;
  406. LOG("Add FFmpeg Source");
  407. m_bSessionOnGo = true;
  408. URL = buildURL(ZL_CMD_ADD_FFMPEG, streamId, m_pVHost, app, mrl);
  409. LOG(URL);
  410. // ToDo: Check if OK to send nothing for checking Onlines
  411. // int len = 0;
  412. // issuePost( URL, NULL, len );
  413. issueGet(URL);
  414. free(URL);
  415. while (m_bSessionOnGo && timeout--)
  416. {
  417. SLEEP(10);
  418. }
  419. if (! m_bSessionOnGo)
  420. {
  421. ZL_STREAM_INFO * tVal;
  422. tVal = (ZL_STREAM_INFO *) malloc(sizeof(ZL_STREAM_INFO));
  423. if (tVal != NULL)
  424. {
  425. tVal->streamId = streamId;
  426. strncpy(tVal->key, m_pLastValue, 128);
  427. m_vCurrentStreams.push_back(tVal);
  428. LOG("Stream Added Successfully");
  429. LOG(m_pLastValue);
  430. return tVal->key;
  431. };
  432. return NULL;
  433. };
  434. return NULL;
  435. }
  436. else
  437. {
  438. LOG("LIST MEDIA - SESSION BUSY!");
  439. }
  440. };
  441. return NULL;
  442. }
  443. bool protocolZLMEDIA::delStreamProxy(CSTR key)
  444. {
  445. return false;
  446. }
  447. bool protocolZLMEDIA::delAllStreamProxys(void)
  448. {
  449. if (m_bServerIsPresent)
  450. { // Just First Time to set initial flag
  451. uint8_t timeout = 100; // 1 sec.
  452. while (m_bSessionOnGo && timeout--)
  453. {
  454. SLEEP(10);
  455. }
  456. if (! m_bSessionOnGo)
  457. {
  458. char URL[256];
  459. LOG("Restart Server");
  460. m_bSessionOnGo = true;
  461. if (m_bUseSSL)
  462. {
  463. sprintf(URL, "https://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_RESTART_SERVER, m_pSecret);
  464. }
  465. else
  466. {
  467. sprintf(URL, "http://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_RESTART_SERVER, m_pSecret);
  468. };
  469. LOG(URL);
  470. issueGet(URL);
  471. while (m_bSessionOnGo && timeout--)
  472. {
  473. SLEEP(10);
  474. }
  475. return (timeout > 0);
  476. }
  477. else
  478. {
  479. LOG("LIST MEDIA - SESSION BUSY!");
  480. }
  481. };
  482. return false;
  483. }
  484. bool protocolZLMEDIA::listMedia()
  485. {
  486. if (m_bServerIsPresent)
  487. { // Just First Time to set initial flag
  488. uint8_t timeout = 100; // 1 sec.
  489. while (m_bSessionOnGo && timeout--)
  490. {
  491. SLEEP(10);
  492. }
  493. if (! m_bSessionOnGo)
  494. {
  495. char URL[256];
  496. LOG("List Media");
  497. m_bSessionOnGo = true;
  498. if (m_bUseSSL)
  499. {
  500. sprintf(URL, "https://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_MEDIA, m_pSecret);
  501. }
  502. else
  503. {
  504. sprintf(URL, "http://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_MEDIA, m_pSecret); // m_pAccessPoint
  505. };
  506. LOG(URL);
  507. // ToDo: Check if OK to send nothing for checking Onlines
  508. // int len = 0;
  509. // issuePost( URL, NULL, len );
  510. issueGet(URL);
  511. timeout = 100;
  512. while (m_bSessionOnGo && timeout--)
  513. {
  514. SLEEP(10);
  515. }
  516. return (timeout > 0);
  517. }
  518. else
  519. {
  520. LOG("LIST MEDIA - SESSION BUSY!");
  521. }
  522. };
  523. return false;
  524. }
  525. bool protocolZLMEDIA::requestIsFinished()
  526. {
  527. return (m_bSessionOnGo == false);
  528. }
  529. #undef LOG
  530. #undef LOG_ERROR