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

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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. #ifdef OLD_WAY_JSON
  87. Json::Reader reader;
  88. if( !reader.parse( (char *)Package->m_pData, root ) ){
  89. LOG_ERROR( "Error parsing the string" );
  90. m_bSessionOnGo = false;
  91. return false;
  92. };
  93. #else
  94. JSONCPP_STRING err;
  95. Json::CharReaderBuilder builder;
  96. const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
  97. if (!reader->parse((char *)Package->m_pData, (char *)Package->m_pData + Package->m_nSize, &root, &err)) {
  98. LOG_ERROR( "Error parsing the string" );
  99. LOG_ERROR( err.c_str() );
  100. m_bSessionOnGo = false;
  101. return false;
  102. };
  103. #endif
  104. for( Json::Value::const_iterator outer = root.begin() ; outer != root.end() ; outer++ ){
  105. for( Json::Value::const_iterator inner = (*outer).begin() ; inner!= (*outer).end() ; inner++ ){
  106. char tBuffer[256];
  107. sprintf( tBuffer,"Data: %s: %s ", inner.key().asString().c_str(), (*inner).asString().c_str() );
  108. LOG( tBuffer );
  109. if( strstr( inner.key().asString().c_str(), "key" ) != NULL ) {
  110. LOG("New Key Arived");
  111. strncpy( m_pLastValue, (*inner).asString().c_str(), 255 );
  112. };
  113. };
  114. };
  115. } catch (exception e ){
  116. LOG_ERROR("Exception!!");
  117. m_bSessionOnGo = false;
  118. return false;
  119. };
  120. m_bSessionOnGo = false;
  121. return true;
  122. };
  123. // Issue New Online Check but return current value
  124. bool protocolZLMEDIA::isOnline(){
  125. if( !m_bServerIsPresent ) { // Just First Time to set initial flag
  126. if( !m_bSessionOnGo ) {
  127. char URL[256];
  128. uint8_t timeout = 100; // 1 sec.
  129. m_bSessionOnGo = true;
  130. if( m_bUseSSL ) {
  131. sprintf(URL,"https://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_API, m_pSecret );
  132. } else {
  133. sprintf(URL,"http://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_API, m_pSecret ); //m_pAccessPoint
  134. };
  135. LOG( URL );
  136. //ToDo: Check if OK to send nothing for checking Onlines
  137. //int len = 0;
  138. //issuePost( URL, NULL, len );
  139. issueGet( URL );
  140. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  141. };
  142. #ifdef DEBUG
  143. if(m_bServerIsPresent) {
  144. LOG("Server is Online");
  145. } else {
  146. LOG("Server is Onffline");
  147. };
  148. #endif
  149. };
  150. return m_bServerIsPresent;
  151. };
  152. bool protocolZLMEDIA::restartServer(){
  153. return false;
  154. };
  155. bool protocolZLMEDIA::getConfig(){
  156. return false;
  157. };
  158. /* Converts a hex character to its integer value */
  159. char from_hex(char ch) {
  160. return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
  161. }
  162. /* Converts an integer value to its hex character*/
  163. char to_hex(char code) {
  164. static char hex[] = "0123456789abcdef";
  165. return hex[code & 15];
  166. }
  167. /* Returns a url-encoded version of str */
  168. /* IMPORTANT: be sure to free() the returned string after use */
  169. char *url_encode(char *str) {
  170. char *pstr = str, *buf = (char *) malloc(strlen(str) * 3 + 1), *pbuf = buf;
  171. while (*pstr) {
  172. if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
  173. *pbuf++ = *pstr;
  174. else if (*pstr == ' ')
  175. *pbuf++ = '+';
  176. else
  177. *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
  178. pstr++;
  179. }
  180. *pbuf = '\0';
  181. return buf;
  182. }
  183. /* Returns a url-decoded version of str */
  184. /* IMPORTANT: be sure to free() the returned string after use */
  185. char *url_decode(char *str) {
  186. char *pstr = str, *buf = (char *)malloc(strlen(str) + 1), *pbuf = buf;
  187. while (*pstr) {
  188. if (*pstr == '%') {
  189. if (pstr[1] && pstr[2]) {
  190. *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
  191. pstr += 2;
  192. }
  193. } else if (*pstr == '+') {
  194. *pbuf++ = ' ';
  195. } else {
  196. *pbuf++ = *pstr;
  197. }
  198. pstr++;
  199. }
  200. *pbuf = '\0';
  201. return buf;
  202. }
  203. #include <iomanip>
  204. #include <sstream>
  205. #include <string>
  206. using namespace std;
  207. string urlEncode( const string &value ){
  208. ostringstream escaped;
  209. escaped.fill('0');
  210. escaped << hex;
  211. for( string::const_iterator i = value.begin(), n = value.end(); i != n; ++i ){
  212. string::value_type c = (*i);
  213. // Keep alphanumeric and other accepted characters intact
  214. if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
  215. escaped << c;
  216. continue;
  217. };
  218. // Any other characters are percent-encoded
  219. escaped << uppercase;
  220. escaped << '%' << setw(2) << int((unsigned char) c);
  221. escaped << nouppercase;
  222. };
  223. return escaped.str();
  224. };
  225. string urlDecode( string text ){
  226. string escaped;
  227. for( auto i = text.begin(), nd = text.end(); i < nd; ++i ){
  228. auto c = ( *i );
  229. switch( c ){
  230. case '%': if( i[1] && i[2] ){
  231. char hs[]{ i[1], i[2] };
  232. escaped += static_cast<char>(strtol(hs, nullptr, 16));
  233. i += 2;
  234. };
  235. break;
  236. case '+': escaped += ' '; break;
  237. default: escaped += c;
  238. };
  239. };
  240. return escaped;
  241. };
  242. CSTR protocolZLMEDIA::buildURL( uint8_t CMD, uint32_t streamId, CSTR host, CSTR app, CSTR src ){
  243. char tBuffer[2048];
  244. char *a = url_encode( app );
  245. char *s = url_encode( src );
  246. char *h = url_encode( host );
  247. if( a == NULL || h == NULL || s == NULL ) {
  248. if( a != NULL ) free( a );
  249. if( s != NULL ) free ( s );
  250. if( h != NULL ) free( h );
  251. return NULL;
  252. };
  253. switch ( CMD ) {
  254. case 1:{
  255. 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 );
  256. break;
  257. };
  258. case 2:{
  259. if( m_bUseSSL ) { //__defaultVhost__
  260. 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 );
  261. } else {
  262. 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 );
  263. };
  264. break;
  265. };
  266. };
  267. free(a); free(s); free(h);
  268. return strdup(tBuffer);
  269. };
  270. CSTR protocolZLMEDIA::addStreamProxy( uint32_t streamId, CSTR app, CSTR mrl ){
  271. if( m_bServerIsPresent ) { // Just First Time to set initial flag
  272. uint8_t timeout = 100; // 1 sec.
  273. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  274. if( !m_bSessionOnGo ){
  275. char *URL;
  276. LOG("Add Stream Proxy");
  277. m_bSessionOnGo = true;
  278. URL = buildURL( ZL_CMD_ADD_PROXY, streamId, m_pVHost, app, mrl );
  279. LOG( URL );
  280. //ToDo: Check if OK to send nothing for checking Onlines
  281. //int len = 0;
  282. //issuePost( URL, NULL, len );
  283. issueGet( URL );
  284. free( URL );
  285. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  286. if( ! m_bSessionOnGo ) {
  287. ZL_STREAM_INFO *tVal;
  288. tVal = (ZL_STREAM_INFO*) malloc(sizeof(ZL_STREAM_INFO));
  289. if( tVal != NULL ) {
  290. tVal->streamId = streamId;
  291. strncpy( tVal->key, m_pLastValue, 128 );
  292. m_vCurrentStreams.push_back( tVal );
  293. LOG("Stream Added Successfully");
  294. LOG( m_pLastValue );
  295. return tVal->key;
  296. };
  297. return NULL;
  298. };
  299. return NULL;
  300. } else LOG("LIST MEDIA - SESSION BUSY!");
  301. };
  302. return NULL;
  303. };
  304. CSTR protocolZLMEDIA::addFFmpegSource( uint32_t streamId, CSTR app, CSTR mrl ){
  305. if( m_bServerIsPresent ) { // Just First Time to set initial flag
  306. uint8_t timeout = 100; // 1 sec.
  307. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  308. if( !m_bSessionOnGo ){
  309. char *URL;
  310. LOG("Add FFmpeg Source");
  311. m_bSessionOnGo = true;
  312. URL = buildURL( ZL_CMD_ADD_FFMPEG, streamId, m_pVHost, app, mrl );
  313. LOG( URL );
  314. //ToDo: Check if OK to send nothing for checking Onlines
  315. //int len = 0;
  316. //issuePost( URL, NULL, len );
  317. issueGet( URL );
  318. free( URL );
  319. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  320. if( ! m_bSessionOnGo ) {
  321. ZL_STREAM_INFO *tVal;
  322. tVal = (ZL_STREAM_INFO*) malloc(sizeof(ZL_STREAM_INFO));
  323. if( tVal != NULL ) {
  324. tVal->streamId = streamId;
  325. strncpy( tVal->key, m_pLastValue, 128 );
  326. m_vCurrentStreams.push_back( tVal );
  327. LOG("Stream Added Successfully");
  328. LOG( m_pLastValue );
  329. return tVal->key;
  330. };
  331. return NULL;
  332. };
  333. return NULL;
  334. } else LOG("LIST MEDIA - SESSION BUSY!");
  335. };
  336. return NULL;
  337. };
  338. bool protocolZLMEDIA::delStreamProxy(CSTR key){
  339. return false;
  340. };
  341. bool protocolZLMEDIA::delAllStreamProxys( void ){
  342. if( m_bServerIsPresent ) { // Just First Time to set initial flag
  343. uint8_t timeout = 100; // 1 sec.
  344. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  345. if( !m_bSessionOnGo ){
  346. char URL[256];
  347. LOG("Restart Server");
  348. m_bSessionOnGo = true;
  349. if( m_bUseSSL ) {
  350. sprintf(URL,"https://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_RESTART_SERVER, m_pSecret );
  351. } else {
  352. sprintf(URL,"http://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_RESTART_SERVER, m_pSecret );
  353. };
  354. LOG( URL );
  355. issueGet( URL );
  356. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  357. return ( timeout > 0 );
  358. } else LOG("LIST MEDIA - SESSION BUSY!");
  359. };
  360. return false;
  361. };
  362. bool protocolZLMEDIA::listMedia(){
  363. if( m_bServerIsPresent ) { // Just First Time to set initial flag
  364. uint8_t timeout = 100; // 1 sec.
  365. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  366. if( !m_bSessionOnGo ) {
  367. char URL[256];
  368. LOG("List Media");
  369. m_bSessionOnGo = true;
  370. if( m_bUseSSL ) {
  371. sprintf(URL,"https://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_MEDIA, m_pSecret );
  372. } else {
  373. sprintf(URL,"http://%s:%d/%s?secret=%s", m_pIP, m_nPort, ZL_LIST_MEDIA, m_pSecret ); //m_pAccessPoint
  374. };
  375. LOG( URL );
  376. //ToDo: Check if OK to send nothing for checking Onlines
  377. //int len = 0;
  378. //issuePost( URL, NULL, len );
  379. issueGet( URL );
  380. timeout = 100;
  381. while( m_bSessionOnGo && timeout-- ) SLEEP(10);
  382. return ( timeout > 0 );
  383. } else LOG("LIST MEDIA - SESSION BUSY!");
  384. };
  385. return false;
  386. };
  387. bool protocolZLMEDIA::requestIsFinished(){
  388. return (m_bSessionOnGo == false);
  389. };
  390. #undef LOG
  391. #undef LOG_ERROR